简体   繁体   中英

python non blocking recv with pipe between processes?

Seen this line of code but could not find documentation

self.conn.setblocking(0)

The question is, how do you poll a pool of pipes without blocking? Got a parent process that needs to communicate with some unstable child processes and wish to poll and check periodically if they've something to say. Do not wish to block if they decide they need more time before they have something new to say. Will this magically do this?

Creating a pipe will return two connection objects. A connection object offers the polling functionality, where you can check if there is anything to read. Polling functionality allows you to specify a timeout to wait for.

If you have a group of connection objects that you are waiting on, then you can use multiprocessing.connection.wait(), or the non-multiprocessing version of it.

For details , see https://docs.python.org/3/library/multiprocessing.html#multiprocessing.connection.Connection which will show you the connection object details. Look at the poll function

This is most likely what you were looking at: https://docs.python.org/2/library/socket.html#socket.socket.setblocking

You don't give much detail so I'm not exactly sure what you are trying to do, but usually when you have a number of sockets that you want to poll, you will use select (see these examples from PyMOTW).

您可以检查 p.poll(0) 然后如果结果为 True 则管道不为空,您可以在不阻塞的情况下接收数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM