python - 如何在 Python 中创建异步计时器?
问题描述
我想用 Python 创建一个 30 秒的计时器,async
但我不知道该怎么做。
我尝试使用此代码,但它阻止了所有代码:
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
功能运行完美。
解决方案
推荐阅读
- swift - 计算SpriteKit中两个圆之间的碰撞向量?
- apache - 使用 htaccess 将包含特定部分的 URL 重定向到另一个 URL
- powershell - Powershell Create-item 在列表中创建多个文件夹
- python - ModuleNotFoundError:没有名为“win32gui”的模块
- css - 如何从一个属性值平滑过渡到另一个属性值?
- arrays - 声明和初始化动态分配的二维数组的最佳方法是什么?
- wordpress - 是否可以将单个 heroku dyno 用于多个 wordpress 网站?
- javascript - 对象未通过/Fetch-Post 请求/API/NodeJS/浏览器
- r - Extracting data froma weird Excel file
- javascript - 功能组件到类组件错误:无法读取未定义的属性“id”