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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?