question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash when passing named function as an argument

See original GitHub issue

Hello,

I am using Python 3.6.5 with joblib 0.11.1.dev0 (master).

Consider the two files:

# test_imp.py
def foo_imp(i):
    return i+2
# test.py
from joblib import Parallel, delayed

def foo(i):
    return i+1
def wrap(i, func):
    return func(i)

# runs fine
from test_imp import foo_imp
res = Parallel(n_jobs=2)([delayed(wrap)(i, foo_imp) for i in range(10)])
print(res)

# crashes
res = Parallel(n_jobs=2)([delayed(wrap)(i, foo) for i in range(10)])
print(res)

When passing an imported function foo_imp as an argument, the code runs fine. For the locally defined function foo however, I get the following error:

Traceback (most recent call last):
  File "/python/site-packages/joblib/externals/loky/process_executor.py", line 376, in _process_worker
    call_item = call_queue.get(block=True, timeout=timeout)
  File "/python3.6/multiprocessing/queues.py", line 113, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'foo' on <module 'joblib.externals.loky.backend.popen_loky_posix' from '/python/site-packages/joblib/externals/loky/backend/popen_loky_posix.py'>

Is this intended behavior? It might be related to #600.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomMoralcommented, May 22, 2018

I have pushed a fix for this issue in master. It should be faster too when issuing many short tasks.

1reaction
tomMoralcommented, May 10, 2018

You could introspect the object using a function like

def is_in_main(obj):
    mod = getattr(obj, "__module__", "")
    return "__main__" in mod

Be careful that you need to go a bit further to ensure that the unpickling is done properly. You could for instance wrap the objects from main in a class such as

class WrapMainModuleObject:
    def __init__(self, obj):
        self.pickled_obj = dumps(obj)
    def __reduce__(self):
        return loads, (self.pickled_obj,)

Edit: Sorry I ended-up creating the pull request. Feel free to test it and report if you see bad behaviors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing pointer to function changes data and crashes program
On the end of createWeapon() , the wp variable, local variable of createWeapon() , is destroyed. Therefore, the returned pointer points to " ......
Read more >
JavaScript: Why Named Arguments are Better than Positional ...
This pattern of passing objects to functions is called named arguments. It is lot more explicit than positional arguments. To change the radix ......
Read more >
Why is my FormatMessage call crashing trying to read my ...
The bug is that code is reusing the ap parameter without resetting it. The documentation for FormatMessage says about the Arguments parameter:.
Read more >
Name=Value in Function Calls - MATLAB & Simulink
MATLAB ® supports two syntaxes for passing name-value arguments. ... Use the name=value syntax to help identify name-value arguments for functions and to...
Read more >
Functions - TypeScript: Handbook
We can add types to each of the parameters and then to the function itself to ... The compiler will build an array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found