Python基礎 程序間通訊

2021-08-03 01:33:57 字數 3859 閱讀 4127

程序間通訊

windows下程序間通訊有很多種,例如:訊息佇列、共享記憶體、管道等等。

python的multiprocessing模組包裝了底層的機制,提供了queue、pipes等多種方式來交換資料。

multiprocessing.pipe()即管道模式,呼叫pipe()返回管道的兩端的connection。

python官方文件的描述:

returns a pair (conn1, conn2) of connection objects representing the ends of a pipe.

因此, pipe僅僅適用於只有兩個程序一讀一寫的單雙工情況,也就是說資訊是只向乙個方向流動。

下面看乙個例子:

# coding=utf-8

import os

from multiprocessing import pipe, process

defsub_process

(parentid, pipe):

print('子程序執行 %s 父程序 %s' % (os.getpid(), parentid))

out_pipe, in_pipe = pipe

in_pipe.close()

while

true:

try:

msg = out_pipe.recv()

print('out pipe : %s' % msg)

# out_pipe.send(msg*10)

except eoferror:

break

if __name__ == '__main__':

print('主程序執行 %s' % os.getpid())

out_pipt, in_pipe = pipe(true)

subprocess = process(target=sub_process, args=(os.getpid(), (out_pipt, in_pipe)))

subprocess.start()

# 關閉主程序的輸出端

out_pipt.close()

for i in range(10):

in_pipe.send(i)

# print('in pipe : %s' % in_pipe.recv())

in_pipe.close()

subprocess.join()

print('主程序結束 %s' % os.getpid())

#列印結果

主程序執行 4288

子程序執行 4964 父程序 4288

out pipe : 0

out pipe : 1

out pipe : 2

out pipe : 3

out pipe : 4

out pipe : 5

out pipe : 6

out pipe : 7

out pipe : 8

out pipe : 9

主程序結束 4288

當然不關閉輸入輸出管道也可以,但是需要自己做同步。

總結一下:

上面的**中主要用到了pipe的send()、recv()、close()方法。當pipe的輸入端被關閉,且無法接收到輸入的值,那麼就會丟擲eoferror。

新建乙個pipe(duplex)的時候,如果duplex為true,那麼建立的管道是雙向的;如果duplex為false,那麼建立的管道是單向的。

queue據官方文件也是基於pipe的實現。

queue的使用主要是一邊put(),一邊get().但是queue可以是多個process 進行put操作,也可以是多個process進行get()操作。

# coding=utf-8

import os

from multiprocessing import queue, process

import random

defgetter

(name, queue):

print('subprocess %s : %s' % (name, os.getpid()))

while

true:

try:

value = queue.get(true, 10)

# block為true,就是如果佇列中無資料了。

# |—————— 若timeout預設是none,那麼會一直等待下去。

# |—————— 若timeout設定了時間,那麼會等待timeout秒後才會丟擲queue.empty異常

# block 為false,如果佇列中無資料,就丟擲queue.empty異常

print('subprocess %s : get value %f' % (name,value))

except exception:

break

defputter

(name, queue):

print('subprocess %s : %s' % (name, os.getpid()))

for i in range(0,5):

value = random.random()

queue.put(value)

# 放入資料 put(obj[, block[, timeout]])

# 若block為true,如佇列是滿的:

# |—————— 若timeout是預設none,那麼就會一直等下去

# |—————— 若timeout設定了等待時間,那麼會等待timeout秒後,如果還是滿的,那麼就丟擲queue.full.

# 若block是false,如果佇列滿了,直接丟擲queue.full

print('subprocess %s : put value %f' % (name,value))

if __name__ == '__main__':

print('parent process run %s ' % os.getpid())

queue = queue(2)#佇列大小為2

getter_process = process(target=getter, args=('getter', queue))

putter_process = process(target=putter, args=('putter', queue))

getter_process.start()

putter_process.start()

#列印結果

parent process run 8852

subprocess getter : 5480

subprocess putter : 8732

subprocess putter : put value 0.953115

subprocess putter : put value 0.281539

subprocess getter : get value 0.953115

subprocess putter : put value 0.459467

subprocess getter : get value 0.281539

subprocess putter : put value 0.639799

subprocess getter : get value 0.459467

subprocess putter : put value 0.114373

subprocess getter : get value 0.639799

subprocess getter : get value 0.114373

Python多程序 程序間通訊

1.使用multiprocessing模組建立的程序之間的通訊 coding utf 8 queue類常用屬性和方法 init self,maxsize 1 qsize full empty put obj,block true,timeout none put nowait obj get blo...

python中程序間通訊

程序間通訊 磁碟互動 速度慢 不安全 socket 本地套接字 管道 訊息列隊 共享記憶體 訊號 訊號量 套接字 管道通訊 pipe 在記憶體中開闢一塊空間,對多個程序可見,通過管道 多個程序進行通訊 multiprocessing pipe fd1,fd2 pipe duplex true 功能 ...

python執行緒 程序間通訊

from multiprocessing import process import os def get process info print info nix系統才有getpid及getppid方法 print process id os.getpid print parent process ...