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, as it is being rescaled. 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. I did manage to make it run with simpler functions on different threads, that just use print
and sleep
. But when it comes to matplotlib
it does not work.
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.
CodePudding user response:
this isn't really because of matplotlib itself, but due to the backends matplotlib uses to draw the GUI, most backends are single-threaded, starting the figure on the other thread may cause the gui eventloop to run on the other thread (depending on the backend), which allows it to work independent from the main thread.
import matplotlib.pyplot as plt
import concurrent.futures
import matplotlib
matplotlib.use("tkAgg") # lock tk backend
voltage = list()
stop_flag = False
def plotter(arr):
fig, ax = plt.subplots()
fig.show()
while True:
# prevent tcl interpreter from crashing
if stop_flag or not plt.fignum_exists(fig.number):
print("stream interupted")
return
arr.append(1)
ax.clear()
ax.plot(arr)
ax.get_figure().canvas.draw()
ax.get_figure().canvas.flush_events()
plt.pause(1) # run eventloop for 1 second
with concurrent.futures.ThreadPoolExecutor() as executor:
plot_thread = executor.submit(plotter, voltage)
for _ in range(15):
res = input("Type: ")
if res == "q":
stop_flag = True
break
print(res)
note that matplotlib will still warn you that what you are trying to do may fail (which again depends on the backend).