Python thread process [Event, queue, process [not finished]]

Source: Internet
Author: User
Tags redis server

Event:

Because each thread is running independently, if a thread's run needs to run its own next operation through the running state of another thread. The event object is required to support this operation. It runs the thread waiting for some time to occur, and the signal flag in the initial object is set to False. If a thread is waiting for an event object, if the signal flag bit is always false, the thread waits until the signal flag becomes true. If the signal flag is true, the thread ignores this setting and continues to run.

Usage:
Event.isset (): Returns the status value of the event, event.wait (): If Event.isset ()= =false will block the thread; Event.set (): Sets the status value of event to True, All blocking pool threads are activated into a ready state, waiting for the operating system to dispatch; Event.clear (): The status value of recovery event is false.
 Example:
Importthreading,timeevent=Threading. Event () #创建对象deffoo (): while notEvent.is_set (): #如果event为False状态, then the status of the event is printed once every two secondsPrint("wait ... ..") event.wait (2The #括号内如果可以写数字 unit is seconds, indicating that no matter whether the signal flag is true or not, just wait 2 seconds to continue running#event.wait () # If the flag bit inside the event object is False, blocking, default is False Print("Connect to Redis server") forIinchRange (5): T=threading. Thread (target=foo,args=()) #创建五个线程 T.start () #启动线程Print("attempt to start Redis server") Time.sleep (6) #暂停主进程 6 seconds Event.set ()#set flag bit to True

Queue:

A data structure similar to a list, with internal locks guaranteeing a thread-safe data structure.

Classification:

Advanced First Out

Advanced Post-out

ImportQueueq= Queue. Queue (3)#FIFO FirstQ = queue. Queue (3)#The number 3 indicates that there are up to 3 values in the queue, and if more than 3 are taken out of the queue, a data is fetched in the putPrint(q) q.put (11)#adding data into the queueQ.put ('Hello') Q.put (54.2) Q.put (68879879)Print(Q.get ()) Q.put (979879)Print(Q.get ())#Remove DataPrint(Q.get ())Print(Q.get ())Print(Q.get ()) Q.join ()#block the process until all tasks are complete and need to mate with Task_done ()ImportThreadingdeffoo (): Q.put (11111111) Q.put (222222) #向队列传入数据 Q.put (33333333Q.join () #例如这里传入队列有是哪个数据, the join will only assume that the task is complete when it receives three task_done. Print('OK')defBar ():Print(Q.full ()) #队列是够满Print(Q.qsize ()) #队列长度Print(Q.get ()) #获取队列数据#Q.task_done ()    Print(Q.get ())#Q.task_done ()    Print(Q.get ())#Q.task_done ()    Print(Q.qsize ()) Q.task_done () Q.task_done () #发送三次 task_done q.task_done () T1= Threading. Thread (target=foo,args=()) #开两个进程 one incoming data to the queue, one to fetch the data from the queue T2= Threading. Thread (target=bar,args=()) T1.start () T2.start () Advanced back outImportQueueq= Queue. Lifoqueue ()#Post-LIFO (Advanced post-exit)Q.put (1) #出入数据q. Put (2) Q.put (3) while  notq.empty (): #如果不Print(Q.get ())
Output by order:ImportQueueq=queue. Priorityqueue () q.put ([1,"1111"])#if it is in the form of a list, it will be compared from left to right, for example here: the first digit of the list is a number, then it is sorted according to 0-9, if there is the same number, then the order of the values is the string 0-9 a-Z
#中文的话, first convert the Chinese into bytes, and then make the same order as the ASCII
Q.put ([5,'22222222']) q.put ([8,'3333333']) q.put ([3,'44444444']) Priority is compared by the ASCII code size rule q.put ('a') Q.put ('v') Q.put ('C') Q.put ('1555') Q.put ('777') while notq.empty (): #如果队列不为空就循环取出队列中的消息Print(Q.get ())

"Producers" and "consumers":

Producer consumption model through a container to solve the coupling between producer and consumer files, producers and consumers do not communicate directly, but through the queue to communicate, so producer production data do not need to wait for the consumer processing, directly into the queue, consumers directly take the queue to fetch data.

Producer: Model for creating data

Consumer: A model of consumer data

Example:

ImportQueue,threading,timeImportRANDOMQ=queue. Queue ()defProducer (): #创建一个厨师对象 count=1 whileCount<10: #做包子 Join the queue for each oneifQ.qsize () <20: #如果队列中的包子不超过20个就继续做 s=random.randint (1,100) Q.put (s)Print("Has made Baozi%s"%s) time.sleep (1) #睡一秒 Count+=1 #计数器加一defConsumer (ID): #消费者 while1: #消费者一直取包子, each steamed bun on Sleep2 s=Q.get ()Print("Consumer"+id+"Has eat%s"%s) time.sleep (2) forIinchRange (5): #一次五个厨师 (thread) at the same time make buns T1=threading. Thread (target=producer,args=()) T1.start () forIinchRange (2): #每次来两个客人 (thread) remove Bun T=threading. Thread (target=consumer,args=(str (i),)) T.start ()

Process:

Multiprocessing module:

Through the multiprocessing module we can create a number of sub-processes to run the program, to achieve the purpose of accelerated computing-intensive programs. can be computed simultaneously using multiple CPU cores. Make up for the lack of python threads. But process switching is far more complex than threads, so it's not possible to use processes without processes

Multi-process:
Advantages:
Using multicore to achieve parallel operations
Disadvantages:
1. Too much overhead
2. Communication difficulties

#实验下列计算时间
ImportMultiprocessingImport Timedeffoo (n): #计算加法和 ret=0 forIinchrange (n): Ret+=IPrint(ret)defbar (N): #计算乘法 ret=1 forIinchRange (1, N): Ret*=IPrint(ret)if __name__=='__main__': The #Windows creation process must be created under process s=time.time ()# #foo (100000000) #一个一个计算, takes a long time #Bar (100000)P1= multiprocessing. Process (target=foo,args= (100000000,)) #使用多进程方式计算 takes much less time than one way to calculate P1.start () P2= multiprocessing. Process (target=bar,args= (100000,) ) #进程普通使用方式跟线程类似 P2.start () p1.join () P2.join ( )Print(Time.time ()-s)

PID of the process:

 fromMultiprocessingImportProcess #调用process模块ImportOSdefinfo (name):Print("Name:", name)Print('Parent Process:', Os.getppid ()) #查看父进程 PIDPrint('Process ID:', Os.getpid ()) #查看子进程 PIDPrint("------------------")if __name__=='__main__': #Windows下进程创建必须在 If __name__ is created without error P1= Process (Target=info, args= ('Process Test',)) #实例化进程 P1.start () #启动进程 p1.join ()

Python thread process [Event, queue, process [not finished]]

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.