I'd like to call exec in an async function and do something like the following code (which is not valid):
import asyncio
async def f():
await exec('x = 1\n' 'await asyncio.sleep(x)')
More precisely, I'd like to be able to wait for a future inside the code that runs in exec.
How can this be achieved?
This is based off @YouTwitFace's answer, but keeps globals unchanged, handles locals better and passes kwargs. Note multi-line strings still won't keep their formatting. Perhaps you want this?
async def aexec(code, **kwargs):
# Don't clutter locals
locs = {}
# Restore globals later
globs = globals().copy()
args = ", ".join(list(kwargs.keys()))
exec(f"async def func({args}):\n " + code.replace("\n", "\n "), {}, locs)
# Don't expect it to return from the coro.
result = await locs["func"](**kwargs)
try:
globals().clear()
# Inconsistent state
finally:
globals().update(**globs)
return result
It starts by saving the locals. It declares the function, but with a restricted local namespace so it doesn't touch the stuff declared in the aexec helper. The function is named func
and we access the locs
dict, containing the result of the exec's locals. The locs["func"]
is what we want to execute, so we call it with **kwargs
from aexec invocation, which moves these args into the local namespace. Then we await this and store it as result
. Finally, we restore locals and return the result.
Warning:
Do not use this if there is any multi-threaded code touching global variables. Go for @YouTwitFace's answer which is simpler and thread-safe, or remove the globals save/restore code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With