directory

  • A preface.
    • 1. Use Queue to communicate between threads
    • 2. Queue is used for communication between multiple processes
    • 3. Pipe interprocess communication, suitable for communication between two processes (one to one)
  • Python interprocess communication Queue/Pipe
    • 1. Use Queue to communicate between processes
    • 2. Use Pipe to communicate between processes
  • Queue.Queue to complete the communication between processes.
  • Four. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

A preface.

Thread threading shares the memory address of the threading Process. The Peocess and the Process are independent of each other (equivalent to deep copy).

2. Communication between threads can be done using the Queue module. Communication between processes can also be done through a Queue, but this Queue is not a thread Queue. Used for communication between parent process and child process or communication between children of the same parent process;

1. Use Queue to communicate between threads

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! Import threading import queue q = queue.queue ()Copy the code

2. Queue is used for communication between multiple processes

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! From multiprocessing import Process from multiprocessing import Queue q = Queue()Copy the code

3. Pipe interprocess communication, suitable for communication between two processes (one to one)

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! From multiprocessing import Process from multiprocessing import Pipe Pipe = Pipe()Copy the code

Python interprocess communication Queue/Pipe

PythonProvides a variety of process communication methods, mainlyThe Queue and PipeQueue is used for communication between multiple processes and Pipe is used for communication between two processes.

1. Use Queue to communicate between processes

  • Put: To insert data into the queue, which also has two optional arguments: blocked and timeout. Self-baidu for details

  • Get: Reads and deletes an element from the queue. There are also two optional parameters: Blocked and timeout, which can be found on your own

#! Usr/bin/env python # -\_- coding: utF-8 \_\_- "" @author: www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" from multiprocessing import Process from multiprocessing import Queue import OS,time,random # Write the code that the data Process executes def proc_write(q,urls): print ('Process is write.... ') for url in urls: q.put(url) print ('put %s to queue... Def proc_read(q): print('Process is reading ') def proc_read(q): print('Process is reading... ') while True: url = q.get(True) print('Get %s from queue' %url) if **name** == '**main**': Create Queue from parent process Q = Queue() proc_write1 = Process(target=proc_write,args=(q,[' URL_1 ',' URL_2 ',' URL_3 ']) proc_write2 = Process(target=proc_write,args=(q,[' URL_4 ','url_5','url_6'])) proc_reader = Process(target=proc_read,args=(q,)) Start () proc_write1.start() proc_write2.start() proc_reader.start() # Wait for proc_write1 to end proc_write1.join() proc_write2.join() Proc_reader. Terminate () print("mian") "" : Process is write.... put url_1 to queue... Process is write.... put url_4 to queue... Process is reading... Get url_1 from queue Get url_4 from queue put url_5 to queue... Get url_5 from queue put url_2 to queue... Get url_2 from queue put url_3 to queue... Get url_3 from queue put url_6 to queue... Get url_6 from queue mian '''Copy the code

2. Use Pipe to communicate between processes

The Pipe method returns (conn1,conn2) representing two ends of a Pipe. The Pipe method has a duplex parameter (True by default). If FALSE, conn1 is only responsible for receiving messages. Conn2 is responsible for sending, and Pipe also contains two methods:

Send: send a message.

Recv: Receives information.

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! """ from multiprocessing import Process from multiprocessing Import Pipe import OS,time,random # Write code that the data Process executes def proc_send(pipe,urls): #print 'Process is write.... ' for url in urls: Print ('Process is send :%s' %url) pipe.send(url) time.sleep(random.random()) def proc_recv(pipe): while True: print('Process rev:%s' %pipe.recv()) time.sleep(random.random()) if __name__ == '__main__': # parent process creates pipe, Pipe = pipe () p1 = Process(target=proc_send,args=(pipe[0],['url_'+ STR (I) for I in range(10)]) p1 = Process(target=proc_send,args=(PIPE [0],['url_'+ STR (I) for I in range(10)] Process(target=proc_recv,args=(pipe[1],)) # P1.start () p2.start() p1.join() p1.terminate () print("mian")"  Process is send :url_0 Process rev:url_0 Process is send :url_1 Process rev:url_1 Process is send :url_2 Process rev:url_2 Process is send :url_3 Process rev:url_3 Process is send :url_4 Process rev:url_4 Process is send :url_5 Process is send :url_6 Process is send :url_7 Process rev:url_5 Process is send :url_8 Process is send :url_9 Process rev:url_6 mian '''Copy the code

Queue.Queue to complete the communication between processes.

We can also use the thread threading Queue to see if the thread can communicate with each other. Example code is as follows:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python interprocess communication Queue/pipe. py @time :2021/05/09 07:37 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" from multiprocessing import Process # from multiprocessing import Queue # Import time def p_put(q,*args): q.put(args) print('Has put %s' % args) def p_get(q,*args): print('%s wait to get... ' % args) print(q.get()) print('%s got it' % args) if __name__ == "__main__": q = queue.Queue() p1 = Process(target=p_put, args=(q,'p1', )) p2 = Process(target=p_get, args=(q,'p2', Traceback (most recent call last): Traceback (most recent call last): File "E:/Project/python_project/untitled10/123.py", line 38, in <module> p1.start() File "G:\ProgramData\Anaconda3\lib\multiprocessing\process.py", line 105, in start self._popen = self._Popen(self) File "G:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "G:\ProgramData\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "G:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__ reduction.dump(process_obj, to_child) File "G:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) TypeError: can't pickle _thread.lock objects '''Copy the code

Four.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python variable argument *argc/**kargcs
  5. Python anonymous function lambda
  6. Python return logic determines expressions
  7. Python is differs from ==
  8. Python mutable and immutable data types
  9. Shallow and deep copies of Python
  10. Python exception Handling
  11. Python thread creation and parameter passing
  12. Python thread mutex Lock
  13. Python thread time Event
  14. The Python thread Condition variable Condition
  15. Python thread Timer Timer
  16. Python thread Semaphore
  17. Python thread Barrier object Barrier
  18. Python thread Queue Queue – FIFO
  19. Python thread queue LifoQueue – LIFO
  20. Python thread PriorityQueue PriorityQueue
  21. Python thread Pool ThreadPoolExecutor
  22. Python thread Pool ThreadPoolExecutor
  23. The Python Process module
  24. The Python Process Process is different from threading
  25. Python interprocess communication Queue/Pipe

Python interprocess communication Queue/Pipe

[Like (1)](javascript:πŸ˜‰ [tip](javascript:πŸ˜‰

This article is published by the blog – Ape Say Programming Ape Say programming!