pyppeteer 异常 RuntimeError: There is no current event loop in thread ‘Thread-3‘.

When creating a child thread and calling pyppeteer, an error RuntimeError: There is no current event loop in thread'Thread-3' is reported.

 

 pyppeteer startup code

loop = asyncio.get_event_loop()
loop.run_until_complete(main(name, password, keyword, isGetDefaultSKU))  # 将协程加入到事件循环loop
loop.close()

 Solution

 

Change the code above to


loop1 = asyncio.new_event_loop() 
asyncio.set_event_loop(loop1)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(name, password, keyword, isGetDefaultSKU))  # 将协程加入到事件循环loop
loop.close()

 

 

 

reference:

https://binglau7.github.io/2018/01/30/%E5%85%B3%E4%BA%8E-asyncio-%E7%9A%84%E5%96%83%E5%96%83%E8%87%AA%E8%AF%AD/

https://zhuanlan.zhihu.com/p/38575715

https://docs.python.org/zh-cn/3/library/asyncio-dev.html#concurrency-and-multithreading

Guess you like

Origin blog.csdn.net/weixin_41822224/article/details/107490323