问题描述:我在Python中进行多处理并使用multiprocessing.Pipe()在进程之间进行通信。我一直在搜索,但仍然无法找到检测管道是否已满的方法。例如下面,writePipe进程继续将数字放入2个不同的管道(奇数和偶数),并且readPipe进程从这2个管道连续读取。但是,从奇数管道读取的速度要快得多,因此偶数管道将满。此时,writePipe进程将被阻塞,而readPipe进程仍在等待从Odd Pipe读取导致死锁。
我的问题:我们有什么方法可以检测到管道是否已满,这样我们可以在仍然运行时停止将数字放入完整的管道并将数字放入仍有空格的管道中?
from multiprocessing import Process, Pipe
def writePipe(sendNumberOdd, sendNumberEven):
i = 0
while True:
if i % 2 == 0:
sendNumberEven.send(i)
else:
sendNumberOdd.send(i)
i += 1
def readPipe(recvNumberOdd, recvNumberEven):
countEven = 0
while True:
countEven += 1
print(countEven, recvNumberEven.recv())
countOdd = 0
while countOdd < 50:
countOdd += 1
print (countOdd, recvNumberOdd.recv())
if __name__ == '__main__':
recvNumberOdd, sendNumberOdd = Pipe(duplex=False)
recvNumberEven, sendNumberEven = Pipe(duplex=False)
write = Process(target=writePipe, args=(sendNumberOdd, sendNumberEven))
read = Process(target=readPipe, args=(recvNumberOdd, recvNumberEven))
write.start()
read.start()
sendNumberOdd.close()
sendNumberEven.close()
答案 0 :(得分:0)
建议未经审核
class Connection(multiprocessing.Connection): def __init__(self, maxsize=0): self.__maxsize = maxsize self.size = 0 self.__lock = multiprocessing.Lock def send(self, obj): with self.__lock: self.size += sizeof(obj) super().send(obj) def recv(self): _recv = super().recv() with self.__lock: self.size -= sizeof(_recv) return _recv def full(self): if self.__maxsize > 0: return self.size >= self.__maxsize return None def Pipe(maxsize=0, duplex=True): return Connection(maxsize), Connection(maxsize)
实施poll()
以检查是否有任何数据准备就绪。
Python»文档:poll([timeout])
Return whether there is any data available to be read.
例如:
if recvNumberEven.poll():
countEven += 1
print(countEven, recvNumberEven.recv())
两者的替代使用wait(...)
multiprocessing.connection.wait(object_list, timeout=None)
Wait till an object in object_list is ready. Returns the list of those objects in object_list which are ready.
答案 1 :(得分:0)
您可以使用df['1'].shift(-1).cumsum().shift(1)
1 NaN
2 31.0
3 61.0
4 92.0
5 122.0
6 153.0
7 184.0
8 214.0
9 245.0
10 275.0
Name: 1, dtype: float64
模块中的select
函数来测试输出管道是否已满。
select