• 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:UsersIBM_ADMINDownloads
    edis-2.4.5-win32-win6464bit  
    
    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)
    
    
    
    
    
    
    
    
    
    
    

  • 相关阅读:
    proc文件系统的简介
    linux获取文件属性(API)
    busybox inittab文件分析
    设计模式之Builder模式
    This用法
    以 280W 数据为依据。对比SQL2008 表分区前和分区后的 T_SQL 效率
    jquery数据验证插件(自制,简单,练手)
    C# 每天温习一点(IEnumerable<TSource>)
    多线程应用扫盲(如何简单使用多线程)
    之前做web性能优化的一些个人心得
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501207.html
Copyright © 2020-2023  润新知