async exec in python












3














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 = 1n' '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?










share|improve this question




















  • 2




    await eval('asyncio.sleep(1))
    – falsetru
    Jul 1 '17 at 14:01












  • I put x=1 on purpose because I really have to call exec and not eval.
    – jerry
    Jul 1 '17 at 18:12






  • 2




    How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
    – falsetru
    Jul 2 '17 at 1:18
















3














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 = 1n' '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?










share|improve this question




















  • 2




    await eval('asyncio.sleep(1))
    – falsetru
    Jul 1 '17 at 14:01












  • I put x=1 on purpose because I really have to call exec and not eval.
    – jerry
    Jul 1 '17 at 18:12






  • 2




    How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
    – falsetru
    Jul 2 '17 at 1:18














3












3








3







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 = 1n' '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?










share|improve this question















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 = 1n' '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?







python python-3.x exec python-asyncio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 1 '17 at 14:03









falsetru

244k33423423




244k33423423










asked Jul 1 '17 at 9:01









jerry

1986




1986








  • 2




    await eval('asyncio.sleep(1))
    – falsetru
    Jul 1 '17 at 14:01












  • I put x=1 on purpose because I really have to call exec and not eval.
    – jerry
    Jul 1 '17 at 18:12






  • 2




    How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
    – falsetru
    Jul 2 '17 at 1:18














  • 2




    await eval('asyncio.sleep(1))
    – falsetru
    Jul 1 '17 at 14:01












  • I put x=1 on purpose because I really have to call exec and not eval.
    – jerry
    Jul 1 '17 at 18:12






  • 2




    How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
    – falsetru
    Jul 2 '17 at 1:18








2




2




await eval('asyncio.sleep(1))
– falsetru
Jul 1 '17 at 14:01






await eval('asyncio.sleep(1))
– falsetru
Jul 1 '17 at 14:01














I put x=1 on purpose because I really have to call exec and not eval.
– jerry
Jul 1 '17 at 18:12




I put x=1 on purpose because I really have to call exec and not eval.
– jerry
Jul 1 '17 at 18:12




2




2




How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
– falsetru
Jul 2 '17 at 1:18




How about this? await eval('asyncio.sleep(x)', globals(), {'x': 1})
– falsetru
Jul 2 '17 at 1:18












3 Answers
3






active

oldest

votes


















1














Here's my solution. No libraries needed including asyncio. 😄



async def execute(code):
exec(
f'async def __ex(): ' +
''.join(f'n {l}' for l in code.split('n'))
)

return await locals()['__ex']()





share|improve this answer



















  • 1




    Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
    – Shree
    Nov 12 '18 at 4:00



















6














Yours problem is that you are trying to await to None object- exec ignores the return value from its code, and always returns None.
If you want to execute and await to the result you should use eval- eval returns the value of the given expression.



Your's code should look like this:



import asyncio

async def f():
exec('x = 1')
await eval('asyncio.sleep(x)')

loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()





share|improve this answer





















  • I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
    – jerry
    Dec 5 '18 at 14:05





















1














Thanks for all the suggestions. I figured out that this can be done with greenlets along async, since greenlets allow performing "top level await":



import greenlet
import asyncio

class GreenAwait:
def __init__(self, child):
self.current = greenlet.getcurrent()
self.value = None
self.child = child

def __call__(self, future):
self.value = future
self.current.switch()

def __iter__(self):
while self.value is not None:
yield self.value
self.value = None
self.child.switch()

def gexec(code):
child = greenlet.greenlet(exec)
gawait = GreenAwait(child)
child.switch(code, {'gawait': gawait})
yield from gawait

async def aexec(code):
green = greenlet.greenlet(gexec)
gen = green.switch(code)
for future in gen:
await future

# modified asyncio example from Python docs
CODE = ('import asyncion'
'import datetimen'

'async def display_date():n'
' for i in range(5):n'
' print(datetime.datetime.now())n'
' await asyncio.sleep(1)n')

def loop():
loop = asyncio.get_event_loop()
loop.run_until_complete(aexec(CODE + 'gawait(display_date())'))
loop.close()





share|improve this answer



















  • 1




    Thank you! This is what I needed as well. Nice to know that others have the same problem.
    – UnsignedByte
    May 13 '18 at 20:21













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44859165%2fasync-exec-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here's my solution. No libraries needed including asyncio. 😄



async def execute(code):
exec(
f'async def __ex(): ' +
''.join(f'n {l}' for l in code.split('n'))
)

return await locals()['__ex']()





share|improve this answer



















  • 1




    Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
    – Shree
    Nov 12 '18 at 4:00
















1














Here's my solution. No libraries needed including asyncio. 😄



async def execute(code):
exec(
f'async def __ex(): ' +
''.join(f'n {l}' for l in code.split('n'))
)

return await locals()['__ex']()





share|improve this answer



















  • 1




    Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
    – Shree
    Nov 12 '18 at 4:00














1












1








1






Here's my solution. No libraries needed including asyncio. 😄



async def execute(code):
exec(
f'async def __ex(): ' +
''.join(f'n {l}' for l in code.split('n'))
)

return await locals()['__ex']()





share|improve this answer














Here's my solution. No libraries needed including asyncio. 😄



async def execute(code):
exec(
f'async def __ex(): ' +
''.join(f'n {l}' for l in code.split('n'))
)

return await locals()['__ex']()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 4:19

























answered Nov 12 '18 at 3:51









YouTwitFace

262




262








  • 1




    Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
    – Shree
    Nov 12 '18 at 4:00














  • 1




    Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
    – Shree
    Nov 12 '18 at 4:00








1




1




Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
– Shree
Nov 12 '18 at 4:00




Welcome to Stack Overflow! Please try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks.
– Shree
Nov 12 '18 at 4:00













6














Yours problem is that you are trying to await to None object- exec ignores the return value from its code, and always returns None.
If you want to execute and await to the result you should use eval- eval returns the value of the given expression.



Your's code should look like this:



import asyncio

async def f():
exec('x = 1')
await eval('asyncio.sleep(x)')

loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()





share|improve this answer





















  • I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
    – jerry
    Dec 5 '18 at 14:05


















6














Yours problem is that you are trying to await to None object- exec ignores the return value from its code, and always returns None.
If you want to execute and await to the result you should use eval- eval returns the value of the given expression.



Your's code should look like this:



import asyncio

async def f():
exec('x = 1')
await eval('asyncio.sleep(x)')

loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()





share|improve this answer





















  • I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
    – jerry
    Dec 5 '18 at 14:05
















6












6








6






Yours problem is that you are trying to await to None object- exec ignores the return value from its code, and always returns None.
If you want to execute and await to the result you should use eval- eval returns the value of the given expression.



Your's code should look like this:



import asyncio

async def f():
exec('x = 1')
await eval('asyncio.sleep(x)')

loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()





share|improve this answer












Yours problem is that you are trying to await to None object- exec ignores the return value from its code, and always returns None.
If you want to execute and await to the result you should use eval- eval returns the value of the given expression.



Your's code should look like this:



import asyncio

async def f():
exec('x = 1')
await eval('asyncio.sleep(x)')

loop = asyncio.get_event_loop()
loop.run_until_complete(f())
loop.close()






share|improve this answer












share|improve this answer



share|improve this answer










answered Jul 2 '17 at 8:16









Yuval Pruss

2,10431535




2,10431535












  • I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
    – jerry
    Dec 5 '18 at 14:05




















  • I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
    – jerry
    Dec 5 '18 at 14:05


















I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
– jerry
Dec 5 '18 at 14:05






I wanted to do it without parsing the string in order to split "x = 1" and "asyncio.sleep(x)"
– jerry
Dec 5 '18 at 14:05













1














Thanks for all the suggestions. I figured out that this can be done with greenlets along async, since greenlets allow performing "top level await":



import greenlet
import asyncio

class GreenAwait:
def __init__(self, child):
self.current = greenlet.getcurrent()
self.value = None
self.child = child

def __call__(self, future):
self.value = future
self.current.switch()

def __iter__(self):
while self.value is not None:
yield self.value
self.value = None
self.child.switch()

def gexec(code):
child = greenlet.greenlet(exec)
gawait = GreenAwait(child)
child.switch(code, {'gawait': gawait})
yield from gawait

async def aexec(code):
green = greenlet.greenlet(gexec)
gen = green.switch(code)
for future in gen:
await future

# modified asyncio example from Python docs
CODE = ('import asyncion'
'import datetimen'

'async def display_date():n'
' for i in range(5):n'
' print(datetime.datetime.now())n'
' await asyncio.sleep(1)n')

def loop():
loop = asyncio.get_event_loop()
loop.run_until_complete(aexec(CODE + 'gawait(display_date())'))
loop.close()





share|improve this answer



















  • 1




    Thank you! This is what I needed as well. Nice to know that others have the same problem.
    – UnsignedByte
    May 13 '18 at 20:21


















1














Thanks for all the suggestions. I figured out that this can be done with greenlets along async, since greenlets allow performing "top level await":



import greenlet
import asyncio

class GreenAwait:
def __init__(self, child):
self.current = greenlet.getcurrent()
self.value = None
self.child = child

def __call__(self, future):
self.value = future
self.current.switch()

def __iter__(self):
while self.value is not None:
yield self.value
self.value = None
self.child.switch()

def gexec(code):
child = greenlet.greenlet(exec)
gawait = GreenAwait(child)
child.switch(code, {'gawait': gawait})
yield from gawait

async def aexec(code):
green = greenlet.greenlet(gexec)
gen = green.switch(code)
for future in gen:
await future

# modified asyncio example from Python docs
CODE = ('import asyncion'
'import datetimen'

'async def display_date():n'
' for i in range(5):n'
' print(datetime.datetime.now())n'
' await asyncio.sleep(1)n')

def loop():
loop = asyncio.get_event_loop()
loop.run_until_complete(aexec(CODE + 'gawait(display_date())'))
loop.close()





share|improve this answer



















  • 1




    Thank you! This is what I needed as well. Nice to know that others have the same problem.
    – UnsignedByte
    May 13 '18 at 20:21
















1












1








1






Thanks for all the suggestions. I figured out that this can be done with greenlets along async, since greenlets allow performing "top level await":



import greenlet
import asyncio

class GreenAwait:
def __init__(self, child):
self.current = greenlet.getcurrent()
self.value = None
self.child = child

def __call__(self, future):
self.value = future
self.current.switch()

def __iter__(self):
while self.value is not None:
yield self.value
self.value = None
self.child.switch()

def gexec(code):
child = greenlet.greenlet(exec)
gawait = GreenAwait(child)
child.switch(code, {'gawait': gawait})
yield from gawait

async def aexec(code):
green = greenlet.greenlet(gexec)
gen = green.switch(code)
for future in gen:
await future

# modified asyncio example from Python docs
CODE = ('import asyncion'
'import datetimen'

'async def display_date():n'
' for i in range(5):n'
' print(datetime.datetime.now())n'
' await asyncio.sleep(1)n')

def loop():
loop = asyncio.get_event_loop()
loop.run_until_complete(aexec(CODE + 'gawait(display_date())'))
loop.close()





share|improve this answer














Thanks for all the suggestions. I figured out that this can be done with greenlets along async, since greenlets allow performing "top level await":



import greenlet
import asyncio

class GreenAwait:
def __init__(self, child):
self.current = greenlet.getcurrent()
self.value = None
self.child = child

def __call__(self, future):
self.value = future
self.current.switch()

def __iter__(self):
while self.value is not None:
yield self.value
self.value = None
self.child.switch()

def gexec(code):
child = greenlet.greenlet(exec)
gawait = GreenAwait(child)
child.switch(code, {'gawait': gawait})
yield from gawait

async def aexec(code):
green = greenlet.greenlet(gexec)
gen = green.switch(code)
for future in gen:
await future

# modified asyncio example from Python docs
CODE = ('import asyncion'
'import datetimen'

'async def display_date():n'
' for i in range(5):n'
' print(datetime.datetime.now())n'
' await asyncio.sleep(1)n')

def loop():
loop = asyncio.get_event_loop()
loop.run_until_complete(aexec(CODE + 'gawait(display_date())'))
loop.close()






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 7 '17 at 3:52

























answered Sep 7 '17 at 3:46









jerry

1986




1986








  • 1




    Thank you! This is what I needed as well. Nice to know that others have the same problem.
    – UnsignedByte
    May 13 '18 at 20:21
















  • 1




    Thank you! This is what I needed as well. Nice to know that others have the same problem.
    – UnsignedByte
    May 13 '18 at 20:21










1




1




Thank you! This is what I needed as well. Nice to know that others have the same problem.
– UnsignedByte
May 13 '18 at 20:21






Thank you! This is what I needed as well. Nice to know that others have the same problem.
– UnsignedByte
May 13 '18 at 20:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44859165%2fasync-exec-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ