Python 线程和 redis 简单读写操作应用

Python 线程和 redis 简单读写操作应用

1. python 线程 & 线程池的概念

python 中使用线程的两种方式: 函数或是用类包装线程对象

函数:调h用thread 模块中的start_new_thread()函数生成新线程,
thread.start_new_thread(function,args[,kwargs])
参数说明:
function ---> 线程函数
args	 ---> 传递给线程函数的参数,必须是tuple类型
kwargs   ---> 可选参数

类方法:使用Threading 模块创建线程,直接从threading.Thread 继承,然后重写 __init__ 方法和run 方法和run

下面给出两种方法的简单实现codes ,在项目上,使用可能更加复杂

#!/usr/bin/python
# -*- coding:utf-8 -*-

import thread
import time

#define a function for thread 

def print_time(threadName ,delay):
	count  = 0
	while count < 5:
		time.sleep(delay)
		count +=1
		print ("%s: %s " % (threadName,time.ctime(time.time()))
		
# define 2 thread

try:
	thread.start_new_thread(print_time,("Thread-1",2,))
	thread.start_new_thread(print_time,("Thread-2",4,))
except:
		print("Error : unable to start thread")
		
while 1 :
	pass
	
	

#==============================================



#!/usr/bin/python
# -*- coding: UTF-8 -*-

import threading 
import time

exitFlag = 0


class myThread(threading,Thread): # 继承父类threading.Thread
	
	def __init__(self ,threadID,name ,counter):
		threading.Thread.__init__(self)
		self.threadID=threadID
		self.name= name
		self.counter = counter
	def run(self):				  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
		print("starting " + self.name )
		print_time(self.name ,self.counter,5)
		print ("Exiting " +self.name)
		
	def print_time(threadName,delay ,counter):
		while counter :
			if exitFlag:
				threading.Thread.exit()
			time.sleep(delay)
			print("%s : %s" % (threadName,time.ctime(time.time()))
			counter -=1

#create new thread
thread1 = myThread(1,"Thread-1",1)
thread2	= myThread(2,"Thread-2",2)

thread1.start()
thread2.start()

print ("Exiting Main Thread")


#==========================================#


线程更深入的内容,需要查阅网络( 线程同步,线程优先级队列)


python  安装 redis
pip install redis

redis 启动 
redis-server.exe  redis.conf
C:\Users\IBM_ADMIN\Downloads\redis-2.4.5-win32-win64\64bit  

python 连接 redis
r = redis.Redis(host='127.0.0.1',port=6379,db=0)

r.set('world','hello')
Ture
r.get('world')

# statistics the number of users in read time 

import time
from redis import Redis
from datetime import datetime
ONLINE_LAST_MINUTES = 5

reids  = Redis()
p = reids.pipeline()
def make_online(user_id):
    now = int(time.time())
    expires = now + ONLINE_LAST_MINUTES * 60 + 10
    all_users_key = 'online-users/%d' % (now // 60)
    user_key = 'user-activity/%s' % user_id
    print(all_users_key ,user_key)

    p.sadd(all_users_key,user_id)
    p.set(user_key,now)
    p.expireat(all_users_key,expires)
    p.expireat(user_key,expires)
    print(p.get(user_key))
    print(p.get(all_users_key))

    print(p.get(user_key))

    #print(expires)



make_online('me')
make_online('you')
#p.execute()

想了解更多请参考网络 
RSHR2.CUST_DIMNSN
CGNS_02.USER_CUST_LIST_EXPLDD
CGNS_02.USER_CUST_LIST_HDR

runstat 的 目的是 收集最新的统计信息,方便优化器选择最合适的访问路径



http://ringcloud.cn.ibm.com/provision/manageCloud

ringcloud has been deprovision (3)











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值