Python: Inter-thread communication & use of multi-process

inter-thread communication
Threads sometimes need to communicate, and the operating system provides many mechanisms to achieve inter-process communication, among which we use the most queue Queue. Classic case: producer and consumer.

The principle of Queue
Queue is a first-in-first-out (First In First Out) queue. A Queue object is created in the main process and passed to the child process as a parameter. Data is put in between the two through put( ), and data is taken out through get( ). After the get() function is executed, the data in the queue will be deleted at the same time, and the Queue of the multiprocessing module can be used to transfer data between multiple processes.

import threading, queue
import time

def produce():
for i in range(10):
time. sleep(0.5)
print('production++++++bread {} {}'.format(threading.current_thread().name, i))
q.put('{}{}'.format(threading.current_thread().name, i))

def consumer():
while True:
time. sleep(1)
# The q.get() method is a blocking method
print('{} bought------bread{}'.format(threading.current_thread().name, q.get()))

q = queue.Queue() # create a q

# a production line
pa = threading.Thread(target=produce, name='pa')
pb = threading.Thread(target=produce, name='pb')
pc = threading.Thread(target=produce, name='pc')

# A consumption line
ca = threading.Thread(target=consumer, name='ca')
cb = threading.Thread(target=consumer, name='cb')
cc = threading.Thread(target=consumer, name='cc')

pa. start()
pb. start()
pc. start()

ca. start()
cb. start()
cc. start()

Use of multiple processes
Program: For example, is a program, which is a static one.

Process: After a program runs, the code + the resources used are called processes, which are the basic unit for resource allocation by the operating system.

Not only can multitasking be done through threads, but processes are also possible.

state of the process
During work, the number of tasks is often greater than the number of CPU cores, that is, some tasks must be executing, while other tasks are waiting for the CPU to execute, thus resulting in different states.

Ready state: The running conditions have been met and are waiting to be executed on the CPU.
Executing state: CPU is executing its function.
Waiting state: Waiting for certain conditions to be met, for example, a program sleeps, and it is in a waiting state at this time.
create process
The multiprocessing module is a cross-platform version of the multi-process module. It provides a Process class to represent a process object. This object can be understood as an independent process that can perform other things.
When creating a child process, you only need to pass in an execution function and function parameters, create a Process instance, and start it with the start() method.

import multiprocessing, time, os

def dance(n):
for i in range(n):
time. sleep(0.5)
print('Dancing {}, pid={}'.format(i, os.getpid()))

def sing(m):
for i in range(m):
time. sleep(0.5)
print('Singing {}, pid={}'.format(i, os.getpid()))

if __name__ == '__main__':
print('The pid of the main process={}'.format(os.getpid()))
# create two processes
# target is used to indicate the task to execute
# args is used to pass parameters, the type is a tuple
p1 = multiprocessing.Process(target=dance, args=(100,))
p2 = multiprocessing.Process(target=sing, args=(100,))

p1. start()
p2. start()
Method Description

Process( target [, name [, args [, kwargs]]])
target: If a function reference is passed, the child process can be tasked to execute the code here
args: the parameters passed to the function specified by target, passed as a tuple
kwargs: Pass named parameters to the function specified by target
name: Set a name for the process, you can not set it
Common methods of instance objects created by Process:

start(): Start a child process instance (create a child process)
is_alive(): Determine whether the process child process is still alive
join([timeout]): Whether to wait for the child process to end, or how many seconds to wait
terminate(): Regardless of whether the task is completed, the child process is terminated immediately
Common properties of instance objects created by Process:

name: the alias of the current process, the default is Process-N, N is an integer incrementing from 1
pid: the pid (process number) of the current process

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us