python 進程間通信Queue/Pipe(42)


?

轉載請注明:猿說Python???python 進程間通信Queue

?

3.python線程threading開創和參變量傳送

2.python進程Process與線程threading差別

猜你喜歡:

1.python進程Process板塊

?

?

?

?
TypeError: can’t pickle _thread.lock objects
????ForkingPickler(file, protocol).dump(obj)
??File “G:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py”, line 60, in dump
????reduction.dump(process_obj, to_child)
??File “G:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py”, line 65, in __init__
????return Popen(process_obj)
??File “G:\ProgramData\Anaconda3\lib\multiprocessing\context.py”, line 322, in _Popen
????return _default_context.get_context().Process._Popen(process_obj)
??File “G:\ProgramData\Anaconda3\lib\multiprocessing\context.py”, line 223, in _Popen
????self._popen = self._Popen(self)
??File “G:\ProgramData\Anaconda3\lib\multiprocessing\process.py”, line 105, in start
????p1.start()
??File “E:/Project/python_project/untitled10/123.py”, line 38, in <module>
Traceback (most recent call last):
14
13
12
11
10
9
8
7
6
5
4
3
2
1

直接異常報錯:
????p2.start()
????p1.start()
????p2 = Process(target=p_get, args=(q,’p2′, ))
????p1 = Process(target=p_put, args=(q,’p1′, ))
????q = queue.Queue()
if __name__ == “__main__”:
?
?
?
?
????print(‘百分之百s got it’ 百分之百 args)
????print(q.get())
?
????print(‘百分之百s wait to get…’ 百分之百 args)
def p_get(q,*args):
?
?
????print(‘Has put 百分之百s’ 百分之百 args)
????q.put(args)
def p_put(q,*args):
?
import time
import queue????????????????????????????# 線程間通信queue.Queue,兩者不要淆惑
# from multiprocessing import Queue???? # 進程間通信Queue,兩者不要淆惑
from multiprocessing import Process
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

三.測試queue.Queue來完成進程間通信能否成功?

當然我們也可以試用線程threading的Queue是否能完成線程間通信,示例代碼如次:

?
mian
Process rev:url_6
Process is send :url_9
Process is send :url_8
Process rev:url_5
Process is send :url_7
Process is send :url_6
Process is send :url_5
Process rev:url_4
Process is send :url_4
Process rev:url_3
Process is send :url_3
Process rev:url_2
Process is send :url_2
Process rev:url_1
Process is send :url_1
Process rev:url_0
Process is send :url_0
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

輸出結果:
????print(“mian”)
????p2.terminate()
????p1.join()
?
????p2.start()
????p1.start()
????#啟動子進程,寫入
????p2 = Process(target=proc_recv,args=(pipe[1],))
????p1 = Process(target=proc_send,args=(pipe[0],[‘url_’+str(i) for i in range(10) ]))
????pipe = Pipe()
????#父進程開創pipe,並傳給各個子進程
if __name__ == ‘__main__’:
?
????????time.sleep(random.random())
????????print(‘Process rev:百分之百s’ 百分之百pipe.recv())
????while True:
def proc_recv(pipe):
#讀數據進程的代碼
?
????????time.sleep(random.random())
????????pipe.send(url)
????????print (‘Process is send :百分之百s’ 百分之百url)
?
????for url in urls:
????#print ‘Process is write….’
def proc_send(pipe,urls):
#寫數據進程執行的代碼
import os,time,random
from multiprocessing import Pipe
from multiprocessing import Process
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1


recv() : 收繳信息;

send() : 發送信息;

2.使役Pipe進程間通信

Pipe常用於兩個進程,兩個進程作別位於管道的兩端 * Pipe辦法回返(conn1,conn2)代表一個管道的兩個端,Pipe辦法有duplex參變量,默認為True,即全雙工模式,若為FALSE,conn1只負責收繳信息,conn2負責發送,Pipe同等也包含兩個辦法:

?
mian
Get url_6 from queue
put url_6 to queue…
Get url_3 from queue
put url_3 to queue…
Get url_2 from queue
put url_2 to queue…
Get url_5 from queue
put url_5 to queue…
Get url_4 from queue
Get url_1 from queue
Process is reading…
put url_4 to queue…
Process is write….
put url_1 to queue…
Process is write….
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

輸出結果:
????print(“mian”)
????proc_reader.terminate()
????#proc_raader進程是死循環,強制終了
????proc_write2.join()
????proc_write1.join()
????#等待proc_write1終了
????proc_reader.start()
?
????proc_write2.start()
????proc_write1.start()
????#啟動子進程,寫入
????proc_reader = Process(target=proc_read,args=(q,))
????proc_write2 = Process(target=proc_write,args=(q,[‘url_4′,’url_5′,’url_6’]))
????proc_write1 = Process(target=proc_write,args=(q,[‘url_1′,’url_2′,’url_3’]))
????q = Queue()
????#父進程開創Queue,並傳給各個子進程
if __name__ == ‘__main__’:
?
????????print(‘Get 百分之百s from queue’ 百分之百url)
????????url = q.get(True)
????while True:
????print(‘Process is reading…’)
def proc_read(q):
#讀數據進程的代碼
?
????????time.sleep(random.random())
????????print (‘put 百分之百s to queue… ‘ 百分之百url)
????????q.put(url)
????for url in urls:
????print (‘Process is write….’)
def proc_write(q,urls):
#寫數據進程執行的代碼
?
import os,time,random
from multiprocessing import Queue
from multiprocessing import Process
?
“””
@Motto:不積跬步無以至千裡,不積小流無以成江海,手續人生的精彩需要持之以恆地積累!
?
@Time:2019/12/21 21:25
@File:python_process_queue.py
?
@Github:www.github.com
@WeChat Official Account(微信公眾號):猿說python
@Blog(私人博客地址): shuopython.com
@Author:何以解懮
“””
# -*- coding:utf-8 _*-
# !usr/bin/env python
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1


  • get():從隊列讀取而且刪除一個元素。同等,他還有兩個可選參變量:blocked和timeout。詳情自行百度

    1.使役Queue進程間通信,Queue包含兩個辦法:

    • put():以插入數據到隊列中,他還有兩個可選參變量:blocked和timeout。詳情自行百度

      二.python進程間通信Queue/Pipe使役

      python提供了多種進程通信的形式,主要Queue和Pipe這兩種形式,Queue用於多個進程間實行通信,Pipe用於兩個進程的通信;

      ?

      ?
      pipe = Pipe()
      ?
      from multiprocessing import Pipe
      from multiprocessing import Process
      # 導入進程相關板塊
      5
      4
      3
      2
      1

      使役Pipe進程間通信,得用於兩個進程之間通信(一對一):


      ?
      q = Queue()
      ?
      from multiprocessing import Queue
      from multiprocessing import Process
      # 導入進程相關板塊
      5
      4
      3
      2
      1

      使役Queue進程間通信,得用於多個進程之間通信:


      ?
      q = queue.Queue()
      ?
      import queue??
      import threading
      #導入線程相關板塊
      5
      4
      3
      2
      1

      使役Queue線程間通信:


      ?

      2.在線程間通信的時分可以使役Queue板塊完成,進程間通信也可以經過Queue完成,不過此Queue並非線程的Queue,進程間通信Queue是將數據 pickle 後傳給另一個進程的 Queue,用於父進程與子進程之間的通信或同一父進程的子進程之間通信;
      main-content editor-side-new”>

      一.前言

      1.在前一篇文章?python進程Process與線程threading差別?中講到線程threading共享內存地址,進程與進程Peocess之間相互獨立,互不影響(相當於深復印);


  • 發表迴響

    你的電子郵件位址並不會被公開。 必要欄位標記為 *