简体   繁体   中英

Passing args, kwargs, to run_in_executor

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?

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.

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM