alphatwirl / mantichora

A simple interface to Python multiprocessing and threading
BSD 3-Clause "New" or "Revised" License
16 stars 1 forks source link

multiprocessing.joinablequeue error when running demo code in jupyter lab #4

Closed drhades closed 4 years ago

drhades commented 4 years ago

Jupyter Lab: 1.2.6 Python: 3.8.0

Running the following demo code:

截屏2020-03-1118 17 22

Background Jupyter service error logging (and the above cell sinked in running status, cannot be stopped):

Process Worker-2: Process Worker-1: Process Worker-3: Traceback (most recent call last): Traceback (most recent call last): File "~/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/process.py", line 313, in _bootstrap self.run() File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 235, in run self._run_tasks() File "~/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/process.py", line 313, in _bootstrap self.run() File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 250, in _run_tasks message = self.task_queue.get() File "~/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/queues.py", line 116, in get return _ForkingPickler.loads(res) File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 235, in run self._run_tasks() File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 250, in _run_tasks message = self.task_queue.get() File "~/.pyenv/versions/3.8.0AttributeError: Can't get attribute 'task_loop' on <module 'main' (built-in)> Traceback (most recent call last): /lib/python3.8/multiprocessing/queues.py", line 116, in get return _ForkingPickler.loads(res) File "~/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/process.py", line 313, in _bootstrap self.run() File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 235, in run self._run_tasks() File "~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/mantichora/hubmp.py", line 250, in _run_tasks message = self.task_queue.get() AttributeError: Can't get attribute 'task_loop' on <module 'main' (built-in)> File "~/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/queues.py", line 116, in get return _ForkingPickler.loads(res) AttributeError: Can't get attribute 'task_loop' on <module 'main' (built-in)>

TaiSakuma commented 4 years ago

Thank you for letting me know. I'll have a look.

drhades commented 4 years ago

For reference, defining task_loop() in another python file and importing, it'll be ok.

TaiSakuma commented 4 years ago

Yes, I'm just working on this now. The code in this branch might work.

TaiSakuma commented 4 years ago

I released the version 0.9.8.

It works fine in Python 3.8 on Jupyter Notebook in my environment.

Screen Shot 2020-03-14 at 11 42 00 AM

Please let me know if the problem persists.

The cause of the problem appears to be the change in the default start method in multiprocessing in Python 3.8. The default start method was "fork" until Python 3.7. But it changed to "spawn" in Python 3.8. The spawn method has more restrictions on how the main module is written as described in this section of the document for mulriprocessing. The mantichora version 0.9.8 uses the "fork" mode.

drhades commented 4 years ago

@TaiSakuma Thanks for your quick fix and detail explaination!

After upgrading to 0.9.8, the above demo code work well, but I get the following error: image

when using mantichora background, code like:

def task_func(*args)
    for i in atpbar(...):
        do some network things
    return something

def parallel_func()
    split something into K parts
    task_partial = partial(task_func, ...)
    with mantichora() as mcore:
        for i in range(K):
            mcore.run(task_partial, ...)
        mcore.receive_all()

It works fine by 0.9.7.

TaiSakuma commented 4 years ago

Thank you for letting me know. I will look into it.

TaiSakuma commented 4 years ago

I released mantichora version 0.9.9.

I added the option mp_start_method to mantichora() so that uses can choose the start method of multiprocessing. The possible values are fork, spawn, and forkserver. The default is fork. For example, if you would like to use the spawn method, which was used in mantichora v0.9.7 in Python 3.8, you can do so as

mantichora(mp_start_method='spawn')

The spawn and forkserver mothods have more restrictions, which are already mentioned above in this issue.

If you need to use the fork method, you might get the same error of may have been in progress in another thread when fork() was called. If you are getting the same error and you are on MacOS, setting the environment variable OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES might solve the problem as suggested at StackOverflow here, which you might have found already.

Please let me know if this doesn't solve the problem or causes other problems.

Thank you.

TaiSakuma commented 4 years ago

I assume that the issue has been resolved. Please create a new issue if not.