This paper analyzes the Python multi process Programming technology in an instance form, which helps to further python programming techniques. Share to everyone for your reference. The specific analysis is as follows:
In general, we tend to use multiple processes in Python because of the limitations of Python threads, such as multithreading that do not take advantage of multi-core CPUs. However, we also use multithreading in scenarios such as Non-blocking asynchronous UI. This article focuses on the Python multi-process problem.
Python introduces a multi process mechanism in 2.6 and provides a wealth of components and APIs to facilitate the writing of concurrent applications. Component process, Queue, Pipe, lock and other components of multiprocessing package provide functionality similar to multithreading. With these components, you can easily write multiple-process concurrent programs.
Process
The use of process is a bit like java.lang.Thread, but thread is threading. The Start method is used to start a process. A simple example:
From multiprocessing import Process
import OS
import time
def sleeper (name, seconds):
print Process ID #%s '% (Os.getpid ())
print ' Parent Process id#%s '% (Os.getppid ())
print '%s'll sleep for%s seconds '% (name, seconds)
time.sleep (seconds)
if __name__ = = "__main__":
Child_proc = Process (Target=sleeper, args= (' Bob ') , 5))
Child_proc.start ()
print "in parent process after child process start"
print parent process Abount T o Join child process "
child_proc.join ()
print" in parent process after child process join "
print" the Paren T ' parent process:%s '% (Os.getppid ())
Instantiating a process requires target and args to be specified. Target is the entry method for the new process, which can be considered the main method. Args is the parameter list for the method. The START process is similar to starting thread, and you must call the Start method. You can also inherit the process, override the Run method, and implement the logic of the procedure in the Run method. Calling the Join method blocks the current calling process until the calling process finishes running.
By manually terminating a process, you can invoke the Terminate method, which sends a sigterm semaphore in a Unix system, and the TerminateProcess method is used in a Windows system. It should be noted that the exit processing logic is not executed, and that the subprocess of the process will not be terminated, and that they will only become orphan processes.
Queue
A queue is a multiple-process-safe cohort that enables data transfer between multiple processes using a queue. The Put method is used to insert data into the queue, and the put method has two optional parameters: Blocked and timeout. If blocked is true (the default) and timeout is positive, the method blocks the time specified by timeout until the queue has the remaining space. If the timeout occurs, a Queue.full exception is thrown. If blocked is false, but the queue is full, the Queue.full exception is thrown immediately.
The Get method can read from the queue and delete an element. Similarly, the Get method has two optional parameters: Blocked and timeout. If blocked is true (the default) and timeout is positive, then no elements are fetched within the wait time, and Queue.empty exceptions are thrown. If blocked is false, there are two situations where if a queue has a value available, the value is returned immediately, or the Queue.empty exception is thrown immediately if the queue is empty. A sample code for the queue:
From multiprocessing import Process, queue
def offer (queue):
queue.put ("Hello World")
def test (queue, num) :
queue.put ("Hello World: + str (num)")
if __name__ = = ' __main__ ':
q = Queue ()
P1 = Process (Target=test, a rgs= (q, 1))
P1.start ()
p = Process (Target=offer, args= (Q,))
P.start ()
p2 = Process (Target=test, Args= (q, 2))
P2.start ()
P2 = Process (target=test, args= (q, 3))
P2.start ()
print Q.get
() Print Q.get () print q.get () print
q.get () print
q.close ()
Output:
Hello world:1
Hello World
Hello World:2
None
Pipes
The Pipe method returns (CONN1, Conn2) representing two ends of a pipe. The pipe method has duplex parameters, and if the duplex parameter is true (the default), then the pipe is full-duplex, which means that both conn1 and CONN2 can send and receive. Duplex for FALSE,CONN1 is only responsible for receiving messages, CONN2 is only responsible for sending messages.
The Send and Recv methods are the methods for sending and receiving messages, respectively. For example, in Full-duplex mode, you can invoke conn1.send to send messages, CONN1.RECV receive messages. If there is no message to receive, the Recv method blocks all the time. If the pipe is already closed, then the Recv method throws a eoferror.
From multiprocessing import Process, Pipe
def send (conn):
conn.send ("Hello World")
conn.close ()
if __ name__ = = ' __main__ ':
parent_conn, child_conn = Pipe ()
p = Process (Target=send, args= (Child_conn,))
P.start ()
print parent_conn.recv ()
Synchronous
The multiprocessing package provides condition, Event, Lock, Rlock, semaphore and other components that can be used for synchronization. The following is an example of using lock:
From multiprocessing import Process, lock
def l (lock, num):
lock.acquire ()
print "Hello num:%s"% (num)
lock.release ()
if __name__ = = ' __main__ ':
lock = Lock () for
num in range:
Process (target=l , args= (lock, Num)). Start ()
Summarize
The above is a simple introduction and example of Python multiprocessing library, familiar with Java multi-threaded development students are not very familiar with the Java Concurrency API is very similar, but Javaconcurrency is to deal with multithreading, We can use these APIs directly in the experience of previous Java multithreading.
Interested friends can test and run the example of this article to deepen understanding. It is believed that this article has certain reference value to everyone's study of Python programming.