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.

Post History

71%
+3 −0
Q&A 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...

1 answer  ·  posted 1y ago by kek‭  ·  edited 1y ago by kek‭

#4: Nominated for promotion by user avatar Alexei‭ · 2023-02-04T06:36:01Z (about 1 year ago)
#3: Post edited by user avatar kek‭ · 2023-02-03T21:17:29Z (about 1 year ago)
fixed the code in the 2nd snippet to working condition
  • Hello everyone,
  • I am seeking help with understanding why multithreading does not work correctly in this example:
  • ```python
  • 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:
  • ```python
  • 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():
  • 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.
  • Hello everyone,
  • I am seeking help with understanding why multithreading does not work correctly in this example:
  • ```python
  • 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:
  • ```python
  • 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.
#2: Post edited by user avatar kek‭ · 2023-02-03T17:44:26Z (about 1 year ago)
  • Hello everyone,
  • I am seeking help with understanding why multithreading does not work correctly in this example:
  • ```python
  • 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.
  • Hello everyone,
  • I am seeking help with understanding why multithreading does not work correctly in this example:
  • ```python
  • 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:
  • ```python
  • 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():
  • 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: Initial revision by user avatar kek‭ · 2023-02-03T12:03:45Z (about 1 year ago)
Python Parallel plotting and and input reading
Hello everyone,

I am seeking help with understanding why multithreading does not work correctly in this example:

```python
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.