Why do I get the error “RuntimeError: This event loop is already running” when running this code?...











up vote
1
down vote

favorite
2












If I run this code, there is no error



import asyncio  
import time
import types
from datetime import datetime

loop = None

def sleep_func(sec):
time.sleep(sec)
return

async def custom_sleep():
print('SLEEP {}n'.format(datetime.now()))
await loop.run_in_executor(None, sleep_func, 2)

async def factorial(name, number):
f = 1
for i in range(2, number+1):
print('Task {}: Compute factorial({})'.format(name, i))
await custom_sleep()
f *= i
print('Task {}: factorial({}) is {}n'.format(name, number, f))



if __name__ == '__main__':

start = time.time()
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(factorial("A", 3)),
asyncio.ensure_future(factorial("B", 4)),
]
loop.run_until_complete(asyncio.wait(tasks))
print('sdfsdfdf')
loop.close()
end = time.time()
print("Total time: {}".format(end - start))


however, if I remove if __name__ == '__main__':, I would get




RuntimeError: This event loop is already running




funny thing is when I get this error, ipython still run and print text that I programmed



why does this error happen?



Add:
My python environment is Anaconda and python 3.7
I executed this code on Spyder(Ipython Kernel)



When I executed code on basic python terminel(not ipython), there was no any error messages



Script name is test_asy.py










share|improve this question















closed as off-topic by Martijn Pieters Nov 9 at 2:10


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Martijn Pieters

If this question can be reworded to fit the rules in the help center, please edit the question.













  • What name did you give your script? How did you run it?
    – Martijn Pieters
    Nov 9 at 2:07










  • I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
    – Martijn Pieters
    Nov 9 at 2:08










  • I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
    – Martijn Pieters
    Nov 9 at 2:12










  • @Martjin Pieters script name is 'test_asy.py'
    – YorHa2B
    Nov 9 at 2:54










  • @Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
    – YorHa2B
    Nov 9 at 2:57

















up vote
1
down vote

favorite
2












If I run this code, there is no error



import asyncio  
import time
import types
from datetime import datetime

loop = None

def sleep_func(sec):
time.sleep(sec)
return

async def custom_sleep():
print('SLEEP {}n'.format(datetime.now()))
await loop.run_in_executor(None, sleep_func, 2)

async def factorial(name, number):
f = 1
for i in range(2, number+1):
print('Task {}: Compute factorial({})'.format(name, i))
await custom_sleep()
f *= i
print('Task {}: factorial({}) is {}n'.format(name, number, f))



if __name__ == '__main__':

start = time.time()
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(factorial("A", 3)),
asyncio.ensure_future(factorial("B", 4)),
]
loop.run_until_complete(asyncio.wait(tasks))
print('sdfsdfdf')
loop.close()
end = time.time()
print("Total time: {}".format(end - start))


however, if I remove if __name__ == '__main__':, I would get




RuntimeError: This event loop is already running




funny thing is when I get this error, ipython still run and print text that I programmed



why does this error happen?



Add:
My python environment is Anaconda and python 3.7
I executed this code on Spyder(Ipython Kernel)



When I executed code on basic python terminel(not ipython), there was no any error messages



Script name is test_asy.py










share|improve this question















closed as off-topic by Martijn Pieters Nov 9 at 2:10


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Martijn Pieters

If this question can be reworded to fit the rules in the help center, please edit the question.













  • What name did you give your script? How did you run it?
    – Martijn Pieters
    Nov 9 at 2:07










  • I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
    – Martijn Pieters
    Nov 9 at 2:08










  • I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
    – Martijn Pieters
    Nov 9 at 2:12










  • @Martjin Pieters script name is 'test_asy.py'
    – YorHa2B
    Nov 9 at 2:54










  • @Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
    – YorHa2B
    Nov 9 at 2:57















up vote
1
down vote

favorite
2









up vote
1
down vote

favorite
2






2





If I run this code, there is no error



import asyncio  
import time
import types
from datetime import datetime

loop = None

def sleep_func(sec):
time.sleep(sec)
return

async def custom_sleep():
print('SLEEP {}n'.format(datetime.now()))
await loop.run_in_executor(None, sleep_func, 2)

async def factorial(name, number):
f = 1
for i in range(2, number+1):
print('Task {}: Compute factorial({})'.format(name, i))
await custom_sleep()
f *= i
print('Task {}: factorial({}) is {}n'.format(name, number, f))



if __name__ == '__main__':

start = time.time()
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(factorial("A", 3)),
asyncio.ensure_future(factorial("B", 4)),
]
loop.run_until_complete(asyncio.wait(tasks))
print('sdfsdfdf')
loop.close()
end = time.time()
print("Total time: {}".format(end - start))


however, if I remove if __name__ == '__main__':, I would get




RuntimeError: This event loop is already running




funny thing is when I get this error, ipython still run and print text that I programmed



why does this error happen?



Add:
My python environment is Anaconda and python 3.7
I executed this code on Spyder(Ipython Kernel)



When I executed code on basic python terminel(not ipython), there was no any error messages



Script name is test_asy.py










share|improve this question















If I run this code, there is no error



import asyncio  
import time
import types
from datetime import datetime

loop = None

def sleep_func(sec):
time.sleep(sec)
return

async def custom_sleep():
print('SLEEP {}n'.format(datetime.now()))
await loop.run_in_executor(None, sleep_func, 2)

async def factorial(name, number):
f = 1
for i in range(2, number+1):
print('Task {}: Compute factorial({})'.format(name, i))
await custom_sleep()
f *= i
print('Task {}: factorial({}) is {}n'.format(name, number, f))



if __name__ == '__main__':

start = time.time()
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(factorial("A", 3)),
asyncio.ensure_future(factorial("B", 4)),
]
loop.run_until_complete(asyncio.wait(tasks))
print('sdfsdfdf')
loop.close()
end = time.time()
print("Total time: {}".format(end - start))


however, if I remove if __name__ == '__main__':, I would get




RuntimeError: This event loop is already running




funny thing is when I get this error, ipython still run and print text that I programmed



why does this error happen?



Add:
My python environment is Anaconda and python 3.7
I executed this code on Spyder(Ipython Kernel)



When I executed code on basic python terminel(not ipython), there was no any error messages



Script name is test_asy.py







python python-3.x python-asyncio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 3:19

























asked Nov 8 at 17:37









YorHa2B

135




135




closed as off-topic by Martijn Pieters Nov 9 at 2:10


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Martijn Pieters

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Martijn Pieters Nov 9 at 2:10


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Martijn Pieters

If this question can be reworded to fit the rules in the help center, please edit the question.












  • What name did you give your script? How did you run it?
    – Martijn Pieters
    Nov 9 at 2:07










  • I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
    – Martijn Pieters
    Nov 9 at 2:08










  • I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
    – Martijn Pieters
    Nov 9 at 2:12










  • @Martjin Pieters script name is 'test_asy.py'
    – YorHa2B
    Nov 9 at 2:54










  • @Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
    – YorHa2B
    Nov 9 at 2:57




















  • What name did you give your script? How did you run it?
    – Martijn Pieters
    Nov 9 at 2:07










  • I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
    – Martijn Pieters
    Nov 9 at 2:08










  • I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
    – Martijn Pieters
    Nov 9 at 2:12










  • @Martjin Pieters script name is 'test_asy.py'
    – YorHa2B
    Nov 9 at 2:54










  • @Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
    – YorHa2B
    Nov 9 at 2:57


















What name did you give your script? How did you run it?
– Martijn Pieters
Nov 9 at 2:07




What name did you give your script? How did you run it?
– Martijn Pieters
Nov 9 at 2:07












I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
– Martijn Pieters
Nov 9 at 2:08




I get a NameError: name 'datetime' is not defined exception here, do make sure you have all your imports in there.
– Martijn Pieters
Nov 9 at 2:08












I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
– Martijn Pieters
Nov 9 at 2:12




I can't reproduce the problem, actually. You appear to be running the module twice, which usually indicates you gave your script the name of a module you import. Say, you named it types.py, then python types.py would run this as a script (__name__ set to '__main__'), but the line import types would import the script as a module (__name__ set to types), and that's a separate module, so the loop.run_until_complete() is made twice.
– Martijn Pieters
Nov 9 at 2:12












@Martjin Pieters script name is 'test_asy.py'
– YorHa2B
Nov 9 at 2:54




@Martjin Pieters script name is 'test_asy.py'
– YorHa2B
Nov 9 at 2:54












@Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
– YorHa2B
Nov 9 at 2:57






@Martjin Pieters Sorry I forgot put import datetime when I registered this question. I will edit it
– YorHa2B
Nov 9 at 2:57



















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)