2021-11-25

Asyncio.sleep - how to use it?

Can anyone help me out, I'm trying to get the program to pause if the condition is met. But as of now, its not sleeping at all. And I can't wrap my head around why. Im completely new to asyncio

time.sleep() doesnt really work either, so I would prefer to use asyncio. Thanks alot!

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())


from Recent Questions - Stack Overflow https://ift.tt/3nOs5wt
https://ift.tt/eA8V8J

No comments:

Post a Comment