In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of an example analysis of interprocess communication in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Overview of the process
A process (Process) is the entity of a running program in a computer. Unlike a program, the program itself is only a description of the organizational form of instructions, data and devices, and the process is the real running entity of the program (those instructions and data). For example, when QQ is not open, QQ is just a program. After opening it, the operating system starts a process for QQ. Open another QQ and start another process.
So what is the relationship between each process in multiple processes? In fact, each process has its own address space, memory, data stack, and other auxiliary data to record its running status. Let's use an example to verify whether information can be shared directly between processes. The sample code is as follows:
From multiprocessing import Processdef plus (): print ('- child process 1 start -') global g_num g_num + = 50 print ('g_num is% d'%g_num) print ('-child process 1 end -') def minus (): print ('- child process 2 start -') Global g_num g_num-= 50 print ('g_num is% d'%g_num) print ('-child process 2 ends -') g_num = 100 # define a global variable if _ _ name__ = ='_ main__': print ('- main process start -') print ('g_num is% d '% g_num) p1 = Process (target=plus) # instantiation process p1 p2 = Process (target=minus) # instantiation process p2 p1.start () # start process p1 p2.start () # start process p2 p1.join () # wait for the end of p1 process p2.join () # wait for the p2 process to end print ('- main process ends -')
The sample code defines a global variable g_num, creates two child processes to perform different operations on the g_num variable, and outputs the results of the operation. The running results are as follows:
-the main process begins-
G_num is 100
-Sub-process 1 begins-
G_num is 150
-Child process 1 ends-
-Sub-process 2 begins-
G_num is 50
-Child process 2 ends-
-end of the main process-
Process finished with exit code 0
In the above code, two child processes are created, one of which increases the g_num variable by 50 and the other subtracts the g_num variable by 50. However, from the running results, the initial recognition value of the g_num variable in both the parent process and the two child processes is 100, that is to say, the result of the global variable g_num in one process is not transmitted to the next, that is, there is no information shared between processes.
How can we achieve inter-process communication? Python's multiprocessing module wraps the underlying mechanism and provides many ways to exchange data, such as Queue (queue), Pipes (pipeline) and so on.
Introduction to queues
Queue is to imitate the queue in real life. Take Chestnut (non-online ticket purchase method, the former way of buying movie tickets), such as queuing up to buy movie tickets, the newcomers are at the end of the line, the front person buys the ticket and walks away, and the people behind follow. From this we can see the two characteristics of the queue:
The newcomers are all at the end of the line.
The first one leaves the team after the completion, and the last one follows.
Use of multi-process queues
Communication between processes is sometimes needed, and the operating system provides many mechanisms to achieve inter-process communication, such as using the Queue queue of the multiprocessing module to achieve data transmission between multiple processes. Queue itself is a message queuing program, so here's how to use it.
When initializing the Queue () object (for example: q=Queue (num)), if the maximum number of messages that can be received is not specified in parentheses, or if the number is negative, then there is no limit on the number of messages that can be accepted (until the end of memory). The common methods of Queue are as follows:
§Queue.qsize (): returns the number of messages contained in the current queue
§Queue.empty (): returns True if the queue is empty, False otherwise
§Queue.full (): if the queue is full, return True, otherwise return False
§Queue.get ([block [, timeout]]): get a message from the queue and remove it from the queue. The default is True.
If block uses the default value, and timeout (unit second) is not set, and the message queue is empty, the program will be blocked (stopped in the read state) until the message is read from the message queue. If timeou is set, it will wait for timeout seconds. If no message has been read, a "Queue.Empty" exception will be thrown.
If the block value is False and the message queue is empty, a "Queue.Empty" exception will be thrown immediately
§Queue.get_nowait (): equivalent to Queue.get (Flase)
§Queue.put (item, [block [, timeout]]): writes item messages to the queue. The default is True.
If block uses the default value and timeout (unit second) is not set, when the message queue has no room to write, the program will be blocked (stop in the write state) until space is made available from the message queue. If timeout is set, it will wait for timeout seconds. If there is no space, a "Queue.Full" exception will be thrown.
If the block value is False, a "Queue.Full" exception will be thrown immediately when the message queue has no space to write.
Queue.put_nowait (item): equivalent to Queue.put (item,False)
The sample code is as follows:
# coding=utf-8from multiprocessing import Queueif _ _ name__ ='_ main__': q=Queue (3) # initialize a Queue object that can receive up to three put messages q.put ("message 1") q.put ("message 2") print (q.full ()) # return False q.put ("message 3") print (q.full ()) # return True # because the message queue is full The following try will throw an exception. # the first try will wait 2 seconds before throwing an exception, and the second try will immediately throw an exception try: q.put ("message 4", True,2) except: print ("message queue is full" Number of existing messages:% s "% q.qsize () try: q.put_nowait (" message 4 ") except: print (" message queue is full, number of existing messages:% s "% q.qsize ()) # when reading messages, first determine whether the message queue is empty Read if not q.empty () when it is empty: print ('- get a message from the queue--') for i in range (q.qsize ()): print (q.get_nowait ()) # first determine whether the message queue is full, and then write if not q.full (): q.put_nowait ("message 4") if it is not full
The running result of the program is as follows:
False
True
Message queue is full. Number of messages available: 3
Message queue is full. Number of messages available: 3
-get messages from the queue
Message 1
Message 2
Message 3
Remarks
This program can only be run successfully in the Windows environment, the mac system will report an error. I don't know why? And the separate print (q.qsize ()) all reported an error.
Use queues to communicate between processes
We know that multiple processes can be created with multiprocessing.Process and queue operations can be implemented with multiprocessing.Queue. Combine Process and Queue to realize the communication between processes. The sample code is as follows:
From multiprocessing import Process Queueimport time# writes data def write_task (Q): if not q.full (): for i in range (5): message = "message" + str (I) q.put (message) print ("write:% s"% message) # read data from queue def read_task (Q): time.sleep (1) # dormant 1 second while not q.empty (): print ("read:% s"% q.get (True) 2)) # wait 2 seconds If no message has been read, # throws a "Queue.Empty" exception if _ _ name__ = = "_ _ main__": print ("- parent process starts -") Q = Queue () # parent process creates Queue And pass to each child process pw = Process (target=write_task, args= (Q,)) # instantiate the child process writing to the queue, and pass queue pr = Process (target=read_task, args= (Q,)) # instantiate the child process of the read queue, and pass queue pw.start () # promoter process pw, write pr.start () # promoter process pr Read pw.join () # wait for pw to end pr.join () # wait for pr to end print ("- parent process ends -")
In the above code, two child processes are created, one is responsible for writing data to the queue, and the other is responsible for reading data from the queue. In order to ensure that the data can be read from the queue correctly, the waiting time of the process for reading the data is set to 2 seconds. If the data cannot be read after 2 seconds, an exception is thrown. The running results are as follows:
-parent process begins-
Write: message 0
Write: message 1
Write: message 2
Write: message 3
Write: message 4
Read: message 0
Reading: message 1
Reading: message 2
Reading: message 3
Read: message 4
-the parent process ends-
Process finished with exit code 0
Thank you for reading! This is the end of this article on "sample Analysis of Inter-process Communication in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.