簡體   English   中英

Python 命名管道行為

[英]Python Named Pipes Behavior

我主要是一名 PLC 程序員,他的任務是編寫一些代碼以在 Raspberry Pi2B(Raspbian Wheezy)上運行,以從在 RPi 上運行的另一個進程獲取一些數據,並使該數據在 Modbus TCP(PLC 協議)接口上可用. 我有它的工作,但現在正試圖防彈它。 我為 IPC 選擇了命名管道,這就是我的問題。 在我的 Python (v2.7) 示例代碼中,如果我啟動閱讀器,它會打開並轉到 readline 命令並按預期阻塞。 當我去啟動我的 writer 並選擇打開、寫入和關閉管道時,它會按預期執行並將記錄寫入管道。 然而,閱讀器只是坐在那里阻止 readline 命令。 當作者循環返回並詢問是否打開管道時,如果我選擇“y”,我的讀者會吐出在前一個循環中寫入的記錄。 我很高興我得到了我的數據,但不明白為什么在編寫器中“打開”管道會導致讀者當時獲取數據。 我認為在我的作者寫完記錄后我會看到讀取的數據。 另外,我認為在 Writer 代碼中“關閉”管道不會做任何事情,因為我可以在第一次通過邏輯打開管道,寫一條記錄,然后在下一次通過邏輯時我可以選擇不打開管道並仍然成功寫入。

  1. 我在這里缺少什么? (修辭,有點)
  2. 在循環中寫入命名管道的正確方法是什么?
  3. 為什么讀者只在 Writer “打開”管道后才抓取一條記錄(無論它是否在前一次通過循環時打開)?

在此先感謝並善待我......記住,我只是一個被迫進入 Python 世界的老 PLC 程序員!

作家:

#!/usr/bin/env python
import logging
logging.basicConfig(format='%(asctime)s %(message)s') 
log = logging.getLogger()
log.setLevel(logging.DEBUG)
log.debug('Logging has started')

log.debug('DocTest Pipe Writer')
import os, time, sys
PipeName = 'DocTestPipe'

if not os.path.exists(PipeName):
    log.debug('Pipe not present...Creating...')
    os.mkfifo(PipeName, 0777)
    log.debug('Pipe is made')
else:
    log.debug('Pipe already present')
ModbusSeed = 0
while True:
    OpenPipe = raw_input ('Open pipe? y or n')
    if OpenPipe == 'y':
        log.debug('Opening pipe')
        PipeOut = open(PipeName, 'w')
        log.debug('Pipe is open for writing.')
    else:
         log.debug('Chose not to open pipe')   
    DataPipeString = '%05d' % ModbusSeed+','+'%05d' % (ModbusSeed+1)+','+'%05d' % (ModbusSeed+2)+','+ \
        '%05d' % (ModbusSeed+3)+','+'%05d' % (ModbusSeed+4)+','+'%05d' % (ModbusSeed+5)+','+ \
        '%05d' % (ModbusSeed+6)+','+'%05d' % (ModbusSeed+7)+','+'%05d' % (ModbusSeed+8)+','+ \
        '%05d' % (ModbusSeed+9)+'\n'
    print 'Pipe Data to write: '+DataPipeString
    WritePipe=raw_input('Write Pipe? y or n')
    if WritePipe == 'y':
        log.debug('Writing pipe')
        PipeOut.write(DataPipeString)
        log.debug('Pipe is written.')
    ClosePipe = raw_input('Close pipe? y or n')
    if ClosePipe == 'y':
        log.debug('Closing pipe')
        PipeOut.close
        log.debug('Pipe is closed')
    else:
        log.debug('Pipe left open')
    ModbusSeed=ModbusSeed+1

讀者:

#!/usr/bin/env python
import logging
logging.basicConfig(format='%(asctime)s %(message)s') #,filename='DocTestLog')
log = logging.getLogger()
log.setLevel(logging.DEBUG)
log.debug('Logging has started')
log.debug('Modbus Server started.')

import os, time, sys

PipeName = 'DocTestPipe'
if not os.path.exists(PipeName):
    log.debug('Pipe not present...Creating...')
    os.mkfifo(PipeName, 0777)
    log.debug('Pipe is made')
else:
    log.debug('Pipe already present')

log.debug('Open pipe for reading')
PipeIn = open(PipeName, 'r')
log.debug('Pipe is open for reading')

while True:
    #raw_input('Press any key to read pipe')
    log.debug('Reading Line')
    try:
        PipeString = PipeIn.readline() [:-1]
    except:
        print 'Nothing there'
    #PipeString = PipeIn.read()
    log.debug('PipeString = '+PipeString)

經過多次摸索、搜索互聯網和反復試驗,我有了我的答案。 問題的關鍵在於,當我打開管道進行寫入時,我沒有指定“緩沖”參數。 這導致我的管道寫入緩存在緩沖區中的某處,而不是立即寫入管道。 每當我“打開”我的寫管道時,它都會將緩沖區刷新到管道中,然后我的讀者將其撿起。 解決方案是在我的“打開”命令中添加“,0”作為附加參數(PipeOut = open(PipeName, 'w',0) 而不是 PipeOut = open(PipeName, 'w')),從而設置緩沖區大小為零。 現在,當我“寫入”管道時,數據會直接進入管道,並且在刷新之前不會陷入困境。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM