午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    python進(jìn)程通信之匿名管道實(shí)例講解
    來源:易賢網(wǎng) 閱讀:1476 次 日期:2016-06-22 10:32:12
    溫馨提示:易賢網(wǎng)小編為您整理了“python進(jìn)程通信之匿名管道實(shí)例講解”,方便廣大網(wǎng)友查閱!

    匿名管道

    管道是一個(gè)單向通道,有點(diǎn)類似共享內(nèi)存緩存.管道有兩端,包括輸入端和輸出端.對(duì)于一個(gè)進(jìn)程的而言,它只能看到管道一端,即要么是輸入端要么是輸出端.

    os.pipe()返回2個(gè)文件描述符(r, w),表示可讀的和可寫的.示例代碼如下:

    代碼如下:

    #!/usr/bin/python

    import time

    import os

    def child(wpipe):

    print('hello from child', os.getpid())

    while true:

    msg = 'how are you\n'.encode()

    os.write(wpipe, msg)

    time.sleep(1)

    def parent():

    rpipe, wpipe = os.pipe()

    pid = os.fork()

    if pid == 0:

    child(wpipe)

    assert false, 'fork child process error!'

    else:

    os.close(wpipe)

    print('hello from parent', os.getpid(), pid)

    fobj = os.fdopen(rpipe, 'r')

    while true:

    recv = os.read(rpipe, 32)

    print recv

    parent()

    輸出如下:

    代碼如下:

    ('hello from parent', 5053, 5054)

    ('hello from child', 5054)

    how are you

    how are you

    how are you

    how are you

    我們也可以改進(jìn)代碼,不用os.read()從管道中讀取二進(jìn)制字節(jié),而是從文件對(duì)象中讀取字符串.這時(shí)需要用到os.fdopen()把底層的文件描述符(管道)包裝成文件對(duì)象,然后再用文件對(duì)象中的readline()方法讀取.這里請(qǐng)注意文件對(duì)象的readline()方法總是讀取有換行符'\n'的一行,而且連換行符也讀取出來.還有一點(diǎn)要改進(jìn)的地方是,把父進(jìn)程和子進(jìn)程的管道中不用的一端關(guān)閉掉.

    代碼如下:

    #!/usr/bin/python

    import time

    import os

    def child(wpipe):

    print('hello from child', os.getpid())

    while true:

    msg = 'how are you\n'.encode()

    os.write(wpipe, msg)

    time.sleep(1)

    def parent():

    rpipe, wpipe = os.pipe()

    pid = os.fork()

    if pid == 0:

    os.close(rpipe)

    child(wpipe)

    assert false, 'fork child process error!'

    else:

    os.close(wpipe)

    print('hello from parent', os.getpid(), pid)

    fobj = os.fdopen(rpipe, 'r')

    while true:

    recv = fobj.readline()[:-1]

    print recv

    parent()

    輸出如下:

    代碼如下:

    ('hello from parent', 5108, 5109)

    ('hello from child', 5109)

    how are you

    how are you

    how are you

    如果要與子進(jìn)程進(jìn)行雙向通信,只有一個(gè)pipe管道是不夠的,需要2個(gè)pipe管道才行.以下示例在父進(jìn)程新建了2個(gè)管道,然后再fork子進(jìn)程.os.dup2()實(shí)現(xiàn)輸出和輸入的重定向.spawn功能類似于subprocess.popen(),既能發(fā)送消息給子進(jìn)程,由能從子子進(jìn)程獲取返回?cái)?shù)據(jù).

    代碼如下:

    #!/usr/bin/python

    #coding=utf-8

    import os, sys

    def spawn(prog, *args):

    stdinfd = sys.stdin.fileno()

    stdoutfd = sys.stdout.fileno()

    parentstdin, childstdout = os.pipe()

    childstdin, parentstdout= os.pipe()

    pid = os.fork()

    if pid:

    os.close(childstdin)

    os.close(childstdout)

    os.dup2(parentstdin, stdinfd)#輸入流綁定到管道,將輸入重定向到管道一端parentstdin

    os.dup2(parentstdout, stdoutfd)#輸出流綁定到管道,發(fā)送到子進(jìn)程childstdin

    else:

    os.close(parentstdin)

    os.close(parentstdout)

    os.dup2(childstdin, stdinfd)#輸入流綁定到管道

    os.dup2(childstdout, stdoutfd)

    args = (prog, ) + args

    os.execvp(prog, args)

    assert false, 'execvp failed!'

    if __name__ == '__main__':

    mypid = os.getpid()

    spawn('python', 'pipetest.py', 'spam')

    print 'hello 1 from parent', mypid #打印到輸出流parentstdout, 經(jīng)管道發(fā)送到子進(jìn)程childstdin

    sys.stdout.flush()

    reply = raw_input()

    sys.stderr.write('parent got: %s\n' % reply)#stderr沒有綁定到管道上

    print 'hello 2 from parent', mypid

    sys.stdout.flush()

    reply = sys.stdin.readline()#另外一種方式獲得子進(jìn)程返回信息

    sys.stderr.write('parent got: %s\n' % reply[:-1])

    pipetest.py代碼如下:

    代碼如下:

    #coding=utf-8

    import os, time, sys

    mypid = os.getpid()

    parentpid = os.getppid()

    sys.stderr.write('child %d of %d got arg: %s\n' %(mypid, parentpid, sys.argv[1]))

    for i in range(2):

    time.sleep(3)

    recv = raw_input()#從管道獲取數(shù)據(jù),來源于父經(jīng)常stdout

    time.sleep(3)

    send = 'child %d got: [%s]' % (mypid, recv)

    print(send)#stdout綁定到管道上,發(fā)送到父進(jìn)程stdin

    sys.stdout.flush()

    輸出:

    代碼如下:

    child 7265 of 7264 got arg: spam

    parent got: child 7265 got: [hello 1 from parent 7264]

    parent got: child 7265 got: [hello 2 from parent 7264]

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:python進(jìn)程通信之匿名管道實(shí)例講解
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)