Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

+3
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+2
−0

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:

  1. Use main thread for GUI/graphs
  2. 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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »