AttributeError 'DupFd' in 'multiprocessing.resource_sharer' | Python multiprocessing + threading

前端 未结 1 770
执笔经年
执笔经年 2021-01-27 03:10

I\'m trying to communicate between multiple threading.Thread(s) doing I/O-bound tasks and multiple multiprocessing.Process(es) doing CPU-bound tasks. W

相关标签:
1条回答
  • 2021-01-27 03:36

    You are forking an already multi-threaded main-process here. That is known to be problematic in general.

    It is in-fact problem prone (and not just in Python). The rule is "thread after you fork, not before". Otherwise, the locks used by the thread executor will get duplicated across processes. If one of those processes dies while it has the lock, all of the other processes using that lock will deadlock -Raymond Hettinger.

    Trigger for the error you get is apparantly that the duplication of the file-descriptor for the pipe fails in the child process.

    To resolve this issue, either create your child-processes as long as your main-process is still single-threaded or use another start_method for creating new processes like 'spawn' (default on Windows) or 'forkserver', if available.

    forkserver

    When the program starts and selects the forkserver start method, a server process is started. From then on, whenever a new process is needed, the parent process connects to the server and requests that it fork a new process. The fork server process is single threaded so it is safe for it to use os.fork(). No unnecessary resources are inherited.

    Available on Unix platforms which support passing file descriptors over Unix pipes. docs

    You can specify another start_method with:

    multiprocessing.set_start_method(method) Set the method which should be used to start child processes. method can be 'fork', 'spawn' or 'forkserver'.

    Note that this should be called at most once, and it should be protected inside the if name == 'main' clause of the main module. docs

    For a benchmark of the specific start_methods (on Ubuntu 18.04) look here.

    0 讨论(0)
提交回复
热议问题