debugging errors in python multiprocessing

Ricky Robinson

I'm using the Pool function of the multiprocessing module in order to run the same code in parallel on different data.

It turns out that on some data my code raises an exception, but the precise line in which this happens is not given:

Traceback (most recent call last):
  File "my_wrapper_script.py", line 366, in <module>
    main()
  File "my_wrapper_script.py", line 343, in main
    results = pool.map(process_function, folders)
  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 148, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 422, in get
    raise self._value
KeyError: 'some_key'

I am aware of multiprocessing.log_to_stderr() , but it seems that it is useful when concurrency issues arise, which is not my case.

Any ideas?

dano

If you're using a new enough version of Python, you'll actually see the real exception get printed prior to that one. For example, here's a sample that fails:

import multiprocessing

def inner():
    raise Exception("FAIL")

def f():
    print("HI")
    inner()

p = multiprocessing.Pool()
p.apply(f)
p.close()
p.join()

Here's the exception when running this with python 3.4:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "test.py", line 9, in f
    inner()
  File "test.py", line 4, in inner
    raise Exception("FAIL")
Exception: FAIL
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    p.apply(f)
  File "/usr/local/lib/python3.4/multiprocessing/pool.py", line 253, in apply
    return self.apply_async(func, args, kwds).get()
  File "/usr/local/lib/python3.4/multiprocessing/pool.py", line 599, in get
    raise self._value
Exception: FAIL

If using a newer version isn't an option, the easiest thing to do is to wrap your worker function in a try/except block that will print the exception prior to re-raising it:

import multiprocessing
import traceback

def inner():
    raise Exception("FAIL")

def f():
    try:
        print("HI")
        inner()
    except Exception:
        print("Exception in worker:")
        traceback.print_exc()
        raise

p = multiprocessing.Pool()
p.apply(f)
p.close()
p.join()

Output:

HI
Exception in worker:
Traceback (most recent call last):
  File "test.py", line 11, in f
    inner()
  File "test.py", line 5, in inner
    raise Exception("FAIL")
Exception: FAIL
Traceback (most recent call last):
  File "test.py", line 18, in <module>
    p.apply(f)
  File "/usr/local/lib/python2.7/multiprocessing/pool.py", line 244, in apply
    return self.apply_async(func, args, kwds).get()
  File "/usr/local/lib/python2.7/multiprocessing/pool.py", line 558, in get
    raise self._value
Exception: FAIL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Multiprocessing: Handling Child Errors in Parent

From Dev

Why no errors from multiprocessing is reported in Python and how to switch on reporting errors?

From Dev

Python - peewee - Debugging statements - where are the errors logged

From Dev

Python - peewee - Debugging statements - where are the errors logged

From Dev

Recommendations for workflow when debugging Python scripts employing multiprocessing?

From Dev

Multiprocessing with Python?

From Dev

Multiprocessing Remote Server and Socket Errors

From Dev

Debugging ExtJS errors after build

From Dev

Debugging vague iOS runtime errors?

From Dev

multiprocessing in python (going from a for loop to multiprocessing for loop)

From Java

Multiprocessing vs Threading Python

From Dev

Kill Python Multiprocessing Pool

From Dev

Memory Error with Multiprocessing in Python

From Dev

Multiprocessing with python and BaseProcess class

From Dev

Python Multiprocessing Troubleshooting

From Dev

python pyaudio using multiprocessing

From Dev

Python, issue with multiprocessing library

From Dev

Python, Multiprocessing and GUI

From Dev

Python- Multiprocessing Daemon

From Dev

python multiprocessing queue error

From Dev

Python Code Coverage and Multiprocessing

From Dev

Python 2.7 Multiprocessing Barrier

From Dev

python multiprocessing not working?

From Dev

Python Multiprocessing appending list

From Java

Python selenium multiprocessing

From Dev

python multiprocessing/threading cleanup

From Dev

Popen, multiprocessing and daemons with Python

From Dev

Python Multiprocessing combined with Multithreading

From Dev

Python Multiprocessing Early Termination

Related Related

HotTag

Archive