We created our own python interpreter and try to create a function to start the background thread, which periodically calls the function to read the sensor, and then stores this reading inside the circular buffer (implemented using deque with the maximum length).
Below is a shortened version of our implementation. The problem we are facing is that when we call monitor_sensor() from the interpreter to start the background thread, the code in the background thread appears only once, which we executed once (which we determined by looking at the contents of sensor_values ) . Also, when we try to exit the interpreter (using exit() ), the interpreter hangs and never exits.
import code import collections import threading def get_sensor_reading(): return 5.0 sensor_values = {} def monitor_sensor(): sensor_values['sensor_1'] = collections.deque(maxlen=1000) background_thread = threading.Thread(target=run_monitor_sensor, args=[sensor_values, get_sensor_reading]) background_thread.start() def run_monitor_sensor(sensor_values, read_sensor_cmd): while True: reading = read_sensor_cmd() sensor_values['sensor_1'].append(reading) import time time.sleep(1) imported_objects = {'monitor_sensor': monitor_sensor, 'sensor_values': sensor_values} code.interact(local=imported_objects)
An interpreter session dump is shown below, in which we try to start a background thread and look at the stored data. Note that we expect much longer than the 1st dream between attempts to read values โโin sensor_values .
218> python manage.py example Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> monitor_sensor() >>> sensor_values {'sensor_1': deque([5.0], maxlen=1000)} >>> sensor_values {'sensor_1': deque([5.0], maxlen=1000)}
The problem is with calling time.sleep(1) - if we delete this, the buffer will quickly fill in the values. Can someone tell us why this problem arises, and how can we get around it?
Update
We solved the problem by moving import time from the inside of the loop to the module level. There is no clue yet why this fixes the problem, but if someone can answer this question, then he will accept this answer.