美文网首页
python 在子线程中使用协程问题

python 在子线程中使用协程问题

作者: 日常记录地 | 来源:发表于2019-07-29 10:41 被阅读0次

    报错:RuntimeError: There is no current event loop in thread '方法AutomaticLoanSchedule'.

    首先,你得到的AssertionError: There is no current event loop in thread ‘Thread-1’.是因为asyncio程序中的每个线程都有自己的事件循环,但它只会在主线程中为你自动创建一个事件循环。所以如果你asyncio.get_event_loop在主线程中调用一次,它将自动创建一个循环对象并将其设置为默认值,但是如果你在一个子线程中再次调用它,你会得到这个错误。相反,您需要在线程启动时显式创建/设置事件循环:

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    相关文章

      网友评论

          本文标题:python 在子线程中使用协程问题

          本文链接:https://www.haomeiwen.com/subject/xqbvrctx.html