RuntimeError: Es gibt keine aktuelle Ereignisschleife im Thread in async + apschedulerPython

Python-Programme
Anonymous
 RuntimeError: Es gibt keine aktuelle Ereignisschleife im Thread in async + apscheduler

Post by Anonymous »

Ich habe eine asynchrone Funktion und muss sie alle N Minuten mit apscheduller ausführen.
Es gibt unten einen Python-Code

Code: Select all

URL_LIST = ['',
'',
'',
]

def demo_async(urls):
"""Fetch list of web pages asynchronously."""
loop = asyncio.get_event_loop() # event loop
future = asyncio.ensure_future(fetch_all(urls)) # tasks to do
loop.run_until_complete(future) # loop until done

async def fetch_all(urls):
tasks = [] # dictionary of start times for each url
async with ClientSession() as session:
for url in urls:
task = asyncio.ensure_future(fetch(url, session))
tasks.append(task) # create list of tasks
_ = await asyncio.gather(*tasks) # gather task responses

async def fetch(url, session):
"""Fetch a url, using specified ClientSession."""
async with session.get(url) as response:
resp = await response.read()
print(resp)

if __name__ == '__main__':
scheduler = AsyncIOScheduler()
scheduler.add_job(demo_async, args=[URL_LIST], trigger='interval', seconds=15)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
try:
asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
pass
Aber als ich versuchte, es auszuführen, bekam ich die nächste Fehlermeldung

Code: Select all

Job "demo_async (trigger: interval[0:00:15], next run at: 2017-10-12 18:21:12 +04)" raised an exception.....
..........\lib\asyncio\events.py", line 584, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread '_0'.
Könnten Sie mir bitte dabei helfen?
Python 3.6, APScheduler 3.3.1,

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post