asyncio.sleep()-如何使用它



有人能帮我吗?如果满足条件,我正试图让程序暂停。但到目前为止,它根本没有睡觉。我无法理解为什么。我对异步完全陌生

time.sleep((也不起作用,所以我更喜欢使用asyncio。非常感谢!

from python_graphql_client import GraphqlClient
import asyncio
import os
import requests
loop = asyncio.get_event_loop()
def print_handle(data):
print(data["data"]["liveMeasurement"]["timestamp"]+" "+str(data["data"]["liveMeasurement"]["power"]))
tall = (data["data"]["liveMeasurement"]["power"])
if tall >= 1000:
print("OK")
# schedule async task from sync code
asyncio.create_task(send_push_notification(data))
print("msg sent")
asyncio.create_task(sleep())


client = GraphqlClient(endpoint="wss://api.tibber.com/v1-beta/gql/subscriptions")
query = """
subscription{
liveMeasurement(homeId:"fd73a8a6ca"){
timestamp
power

}
}
"""
query2 = """
mutation{
sendPushNotification(input: {
title: "Advarsel! Høyt forbruk",
message: "Du bruker 8kw eller mer",
screenToOpen: CONSUMPTION
}){
successful
pushedToNumberOfDevices
}
}
"""
async def sleep():
await asyncio.sleep(10)
async def send_push_notification(data):
#maybe update your query with the received data here
await client.execute_async(query=query2,headers={'Authorization': "2bTCaFx74"}) 

async def main():
await client.subscribe(query=query, headers={'Authorization': "2bTCaFxDiYdHlxBSt074"}, handle=print_handle)

asyncio.run(main())

如果我理解正确,您希望观察一些数据的广播,并对这些广播做出反应,保留暂停这些反应的权利。类似于:

async def monitor(read_broadcast):
while True:
data = await read_broadcast()
print(data["data"]["liveMeasurement"]["timestamp"]+" "+str(data["data"]["liveMeasurement"]["power"]))
tall = (data["data"]["liveMeasurement"]["power"])
if tall >= 1000:
print("OK")
await send_push_notification(data)
print("msg sent")
# sleep for a while before sending another one
await asyncio.sleep(10)

为了实现read_broadcast;未来":

# client, query, query2, send_push_notification defined as before
async def main():
broadcast_fut = None
def update_broadcast_fut(_fut=None):
nonlocal broadcast_fut
broadcast_fut = asyncio.get_event_loop().create_future()
broadcast_fut.add_done_callback(update_broadcast_fut)
update_broadcast_fut()
def read_broadcast():
return broadcast_fut
asyncio.create_task(monitor(read_broadcast))
await client.subscribe(
query=query, headers={'Authorization': "2bTCaFxDiYdHlxBSt074"},
handle=lambda data: broadcast_fut.set_result(data),
)

asyncio.run(main())

请注意,我还没有测试过上面的代码,所以可能会有拼写错误。

我认为减少发送消息数量的最简单方法是定义一个最小间隔,在该间隔内,当值仍然超过阈值时,不发送通知。

import time

last_notification_timestamp = 0
NOTIFICATION_INTERVAL = 5 * 60  # 5 min
def print_handle(data):
global last_notification_timestamp
print(
data["data"]["liveMeasurement"]["timestamp"]
+ " "
+ str(data["data"]["liveMeasurement"]["power"])
)
tall = data["data"]["liveMeasurement"]["power"]
current_time = time.time()
if (
tall >= 1000
and current_time - NOTIFICATION_INTERVAL > last_notification_timestamp
):
print("OK")
# schedule async task from sync code
asyncio.create_task(send_push_notification(data))
last_notification_timestamp = current_time
print("msg sent")

最后发送的消息的时间戳需要存储在某个地方,因此我们将在全局范围中定义一个变量来保存它,并在print_handle()中使用global关键字,以便能够从函数中写入它。在函数中,我们将检查该值是否高于阈值,以及在最后一条消息之后是否经过了足够的时间。这样,您仍然可以保持订阅的有效性,并限制收到的通知数量。这很简单,但你可能很快就会想扩大你想对收到的数据做什么的范围。请记住,print_handle()是一个同步回调,应该尽可能短。

最新更新