Communication Between Threads | Teach You to Get Started With Python One Hundred and Three
Communication Between Threads | Teach You to Get Started With Python One Hundred and Three
Communication between threads is sometimes required, and the operating system provides many mechanisms to achieve inter-process communication, of which we use the most Queue.
Inter-thread communication
Communication between threads is sometimes required, and the operating system provides many mechanisms to achieve inter-process communication, of which we use the most Queue. Classic case: producer and consumer.
Queue is a First In First Out (First In First Out) queue. A Queue object is created in the main process and passed into 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. You can use the Queue of the multiprocessing module to realize data transfer between
import threading, queue
import time
def produce( ):
for i in range( 10):
time.sleep (0.5)
print ( 'producing ++++++ bread{} {}' .format( threading.current_thread ().name, i ))
q.put ( ' {}{ }' .format( threading.current_thread ().name, i ))
def consumer( ):
while True :
time.sleep (1)
# q. get () method is a blocking method
print ( '{}buy------bread{}' .format( threading.current_thread () . name, q.get ( )))
q = queue.Queue () # create a q
# one production line
pa = threading.Thread ( target =produce, name = 'pa' )
pb = threading.Thread ( target =produce, name = 'pb' )
pc = threading.Thread(target=produce, name='pc')
# 一条消费线
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()
Communication between threads is sometimes required, and the operating system provides many mechanisms to achieve inter-process communication, of which we use the most Queue.
Inter-thread communication
Communication between threads is sometimes required, and the operating system provides many mechanisms to achieve inter-process communication, of which we use the most Queue. Classic case: producer and consumer.
Communication Between Threads.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 into 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. You can use the Queue of the multiprocessing module to realize data transfer between
import threading, queue
import time
def produce( ):
for i in range( 10):
time.sleep (0.5)
print ( 'producing ++++++ bread{} {}' .format( threading.current_thread ().name, i ))
q.put ( ' {}{ }' .format( threading.current_thread ().name, i ))
def consumer( ):
while True :
time.sleep (1)
# q. get () method is a blocking method
print ( '{}buy------bread{}' .format( threading.current_thread () . name, q.get ( )))
q = queue.Queue () # create a q
# one production line
pa = threading.Thread ( target =produce, name = 'pa' )
pb = threading.Thread ( target =produce, name = 'pb' )
pc = threading.Thread(target=produce, name='pc')
# 一条消费线
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()
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
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