Python returning values from infinite loop thread

前端 未结 1 1321
鱼传尺愫
鱼传尺愫 2021-01-23 04:41

So for my program I need to check a client on my local network, which has a Flask server running. This Flask server is returning a number that is able to change.

Now to

相关标签:
1条回答
  • 2021-01-23 05:14

    You need a Queue and something listening on the queue

    import queue
    import threading
    import requests
    from bs4 import BeautifulSoup
    
    def checkClient(q):
        while True:
            page = requests.get('http://192.168.1.25/8080')
            soup = BeautifulSoup(page.text, 'html.parser')
            value = soup.find('div', class_='valueDecibel')
            q.put(value)
    
    q = queue.Queue()
    t1 = threading.Thread(target=checkClient, name=checkClient, args=(q,))
    t1.start()
    
    while True:
        value = q.get()
        print(value)
    

    The Queue is thread safe and allows to pass values back and forth. In your case they are only being sent from the thread to a receiver.

    See: https://docs.python.org/3/library/queue.html

    0 讨论(0)
提交回复
热议问题