Python thread and Redis Simple read-write operation application __python

Source: Internet
Author: User
Tags redis install redis
Python thread and Redis Simple read-write operation Application 1. Python Threads & thread pool Concepts Python uses threads in two ways: functions or wrapping thread object functions with classes: Tuning H generates new threads with the Start_new_thread () function in the thread module, THREAD.START_NEW_THR EAD (Function,args[,kwargs]) parameter description: function---> Thread function args > passed to the thread function, the parameter must be tuple type Kwargs---> Optional parameter class method: using T The Hreading module creates the thread directly from the threading.  Thread inheritance, and then rewrite the __init__ method and the Run method and run below give a simple implementation of the two methods codes, which may be more complex to use on the project #!/usr/bin/python #-*-coding:utf-8-*-Import
		Thread import time #define a function to 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:threa D.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/py Thon #-*-coding:utf-8-*-import Threading Import Time Exitflag = 0 Class Mythread (THREading,thread): # inherits the parent class threading. Thread def __init__ (self, Threadid,name, counter): Threading. Thread.__init__ (self) self.threadid=threadid self.name= name self.counter = Counter def run (self): # write code to execute to The run function inside the thread runs the Run function print directly ("starting" + Self.name) print_time (self.name, self.counter,5) print ("Exiting" +se Lf.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 T Hread thread1 = mythread (1, "Thread-1", 1) thread2 = Mythread (2, "Thread-2", 2) Thread1.start () Thread2.start () print ("Exit ing Main thread ") #==========================================# more in-depth content, requires lookup network (thread sync, thread priority queue) python install redis pip i Nstall redis redis boot redis-server.exe redis.conf C:\Users\IBM_ADMIN\Downloads\redis-2.4.5-win32-win64\64bit python Connection 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/from Redis import Redis F
    ROM 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 * + all_users_key = ' online-users/%d '% (now//6 0) 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)) p Rint (P.get (All_users_key)) print (P.get (user_key)) #print (expires) make_online (' Me ') make_online (' You ') #p. exe Cute () For more information please refer to network RSHR2. The purpose of CUST_DIMNSN Cgns_02.user_cust_list_expldd CGNS_02.USER_CUST_LIST_HDR Runstat is to collect the latest statistics to facilitate the optimizer to choose the most appropriate access path HTTP://R










 Ingcloud.cn.ibm.com/provision/managecloud Ringcloud has been deprovision (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.