Input () blocks use of processes

I have a problem with multiprocessing. If I am waiting for input in a thread the process is not starting.

The class to put the input into a queue in the background:

class InputCatcher(Thread):
    def __init__(self, input_queue):
        self.input_queue = input_queue

    def run(self):
        while True:
            self.input_queue.put(input()) # <<-- Without this it works!

The class that will not start:

class Usb(Process):
    def __init__(self, port, ctrl=Controller()):
        self.usb_port = port
        self.ctrl = ctrl

    def run(self):
        ser = Serial(self.usb_port, 115200)
        while True:
            dsl = ser.readline()
            self.ctrl.get_dataset_queue().put(['USBDS', dsl])

Starting with:

ic = InputCatcher(self.input_queue)

usbs = []
for port in usb_ports():
    if not port in ctrl.xbee_ports:
        usbs.append(Usb(port, ctrl))

for usb in usbs:
    usb.daemon = True

When you call input
, it is blocking the entire Python process, not just the thread it runs in. This happens because reading from STDIN, like reading from any other file-like object, involves making a blocking syscall – that is, input
blocking to wait for user input happens at the OS level, rather than inside Python’s own thread management code. Python threads are essentially invisible to the OS process scheduler, so Python itself
gets blocked.

The usual way around blocking problems like this is to use processes instead of threads. If you make InputCatcher into a process rather than a thread, then it becomes a separate OS-level process that the OS can schedule independently, and so the syscall will only block that
process and not the main one.

, that Python automatically closes STDIN when you spawn a process.

So, you would need to have the producer for the queue in the main process, and only the consumer in another one. This is also a trivial adaption – don’t start the producer (InputCatcher) running until after
all of the consumer processes have spawned. That involves moving the line:


to below the two loops. But in this case, there’s no need for that to be backgrounded at all – it doesn’t run simultaneously with other things. So, you can forget about the InputCatcher class entirely, and just write your code like this:

for usb in usbs:
    usb.daemon = True

while True:

You might also want to consider a particular input – say, the empty string – to end the program. Having the input in the main run makes this really easy by just ending the loop:

while True:
    data = input('Type data; or enter to exit: ')
    if not data:
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Input &lpar;&rpar; blocks use of processes

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录