Passing args, kwargs, to run_in_executor

Question:

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?

Asked By: user10332687

||

Answers:

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.

Answered By: Cyphase

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.

Answered By: Davis Herring

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

Answered By: pintermor9

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.

Answered By: Ron
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.