Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Python Parallel plotting and and input reading
Hello everyone,
I am seeking help with understanding why multithreading does not work correctly in this example:
import time
import matplotlib.pyplot as plt
import concurrent.futures
voltage = list()
stop_flag = False
fig, ax = plt.subplots()
fig.show()
def plotter(ax, arr):
while True:
if stop_flag:
print("stream interupted")
return
arr.append(1)
ax.clear()
ax.plot(arr)
ax.get_figure().canvas.draw()
ax.get_figure().canvas.flush_events()
time.sleep(1)
with concurrent.futures.ThreadPoolExecutor() as executor:
plot_thread = executor.submit(plotter, ax, voltage)
for _ in range(15):
res = input("Type: ")
if res == "q":
stop_flag = True
break
print(res)
Expected behaviour:
Graph is being spawned by matplotlib and the line at y=1
is continuously growing in the x direction. In parallel terminal waits for user input. If input is q
, the function plotter exits and everything finishes.
Actual behaviour: Graph is appearing, but not updating at all, while user input is being processed and reprinted to console. It seems that one point does exist on the graph. When pressing q or reaching 15 iterations program terminates.
I do not understand why parallel thread does not run and updates the graph independently from for
loop.
Edit: Changing plotting to the main program and running input loop in another thread also does not work:
import time
import matplotlib.pyplot as plt
import concurrent.futures
import matplotlib
matplotlib.use("tkAgg") # lock tk backend
voltage = list()
stop_flag = False
fig, ax = plt.subplots()
fig.show()
def fetch_input():
global stop_flag
for _ in range(15):
res = input("Type: ")
if res == "q":
stop_flag = True
break
print(res)
with concurrent.futures.ThreadPoolExecutor() as executor:
new_thread = executor.submit(fetch_input)
while True:
if stop_flag:
print("main loop interupted")
break
voltage.append(1)
ax.clear()
ax.plot(voltage)
ax.get_figure().canvas.draw()
ax.get_figure().canvas.flush_events()
plt.pause(1) # run eventloop for 1 second
# time.sleep(1)
First the function fetching the input starts and continues until it finished and only then the figure appears. It is even weirder now, since fig.show()
happens in the beginning before the thread is even started. So I would expect to see at least empty figure together with input query in the terminal.
1 answer
After moving the plotting while
loop in the second edit inside with
context manager and declaring stop_flag
as a global variable inside fetch_input
it worked as expected.
Lessons learned:
- Use main thread for GUI/graphs
-
with concurrent.futures.ThreadPoolExecutor() as executor:
joins all the threads in the end. So all the code, that should be parallelized must be within context manager or used without context manager.
2nd snippet in question now contains working code.
0 comment threads