2

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 retrieve that value, I use the requests library and BeautifulSoup.
I want to use the retrieved value in another part of my script (while continuously checking the other client). For this I thought I could use the threading module.
The problem is, however, that the thread only returns it's values when it's done with the loop, but the loop needs to be infinite.

This is what I got so far:

import threading
import requests
from bs4 import BeautifulSoup

def checkClient():
    while True:
        page = requests.get('http://192.168.1.25/8080')
        soup = BeautifulSoup(page.text, 'html.parser')
        value = soup.find('div', class_='valueDecibel')
        print(value)

t1 = threading.Thread(target=checkClient, name=checkClient)
t1.start()


Does anyone know how to return the printed values to another function here? Of course you can replace the requests.get url with some kind of API where the values change a lot.

6
  • 1
    Depending on what kind of performance you need, a simple producer-consumer queue may be sufficient. Commented Jan 24, 2018 at 18:53
  • It does not need to be high speed. I have looked in to the Queue module, but I am not sure whether if provides what I need. Besides, I don't really know how to use it(I have already looked in the documentation). Thanks for the answer! Commented Jan 24, 2018 at 18:56
  • 1
    Put the values in a queue and start another thread that removes values from the queue. Commented Jan 24, 2018 at 18:57
  • Ah okay. I will try that. I assume @mementum did this in his code? Commented Jan 24, 2018 at 19:01
  • Are you sure you need the values as fast as possible? Add a sleep() in your loop. Also, are you only interested in the most recent value (current volume) or the whole history? In the latter case consider attaching timestamps to values, because you're not going to receive them at an always consistent rate. Commented Jan 24, 2018 at 19:11

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Two problems arise when the client thread fails to read from the queue for some time. (1) The queue gets filled up with old values, which can be quite many. (2) The current value is the last. Also, not having a sleep(...) inside the infinite loop is a good way to kill that local server with entirely too many requests.
Letting the putter block on put by defining the Queue with a maxsize is a straightforward solution to the problem. I guess the OP wanted first to understand how to cleanly get values in and out of a thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.