如何在Python中创建异步计时器?

时间:2019-04-13 11:07:33

标签: python python-asyncio python-multithreading

我想用async在Python中创建一个30秒的计时器,但我不知道该怎么做。

我尝试使用此代码,但它阻止了所有代码:

def restore_presences(self, loop):
    async def func():
        db, c = self.connect()
        c.execute('SELECT Presenza FROM Teachers')
        presences = c.fetchall()
        for item in presences:
            c.execute('UPDATE Teachers SET Presenza = ?', ('no',))
            db.commit()
        db.close()

    threading.Thread(target=lambda loop: loop.run_until_complete(func()), args=(loop,)).start()


# THIS BLOCKS MY CODE
t = Timer(30, self.restore_presences, args=[asyncio.get_event_loop()])
t.start()
t.join()
# THIS BLOCKS MY CODE

async函数运行完美。

0 个答案:

没有答案