Reputation:
I am trying to pass arguments to run_in_executor
like so:
loop.run_in_executor(None, update_contacts, data={
'email': email,
'access_token': g.tokens['access_token']
})
However, I get the following error:
run_in_executor() got an unexpected keyword argument 'data'
Is there a generic way to pass args to this function?
Upvotes: 36
Views: 26427
Reputation: 441
Keeping with the generic path what I do is loop.run_in_executor(None, lambda data: update_contacts(**data), { 'email': email, 'access_token': g.tokens['access_token'] })
Then I do not need to use any extra imports.
Upvotes: 0
Reputation: 21
Create a function that calls it:
def foo():
update_contacts(data={'email': email,'access_token': g.tokens['access_token']})
loop.run_in_executor(None, foo)
or, the func could also be lambda:
loop.run_in_executor(None, lambda: update_contacts(data={'email': email,'access_token': g.tokens['access_token']})
or use fuctools
Upvotes: 2
Reputation: 39868
You asked for a "generic way"; the most generic answer is that you create a function for the purpose. If the data
you want to provide is local to the caller, you create that function inside the caller, perhaps as a lambda:
loop.run_in_executor(None,lambda: update_contacts(data={
'email': email,
'access_token': g.tokens['access_token']
})
As given, this is not much different from the functools.partial
answer, and (as the documentation says) it might reduce the utility of debug output, but it lets you do things like compute the data
values on the executor and act on the return value from update_contacts
.
Upvotes: 10
Reputation: 12022
Use functools.partial
; it's a standard way to do such things, and it's specifically recommended in the docs for loop.run_in_executor
, as well as more generally in the Event Loop docs.
Here's how it might look for you:
import functools # at the top with the other imports
loop.run_in_executor(None, functools.partial(update_contacts, data={
'email': email,
'access_token': g.tokens['access_token']
}))
You could also do from functools import partial
, if you like.
Upvotes: 59