Skip to content

Instantly share code, notes, and snippets.

@dwf
Created September 16, 2011 19:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwf/1222883 to your computer and use it in GitHub Desktop.
Save dwf/1222883 to your computer and use it in GitHub Desktop.
Using matplotlib + multiprocessing for asynchronous, interactive monitoring plots.
"""Demo of how to pop up plots asynchronously using separate processes."""
from multiprocessing import Process
import time
import sys
import matplotlib.pyplot as plt
import numpy as np
def demo():
i = 0
processes = []
while True:
i += 1
s = time.time()
while time.time() - s < 5:
print 'HA',
sys.stdout.flush()
def do_something():
figno = i
f = plt.figure()
# Normally this will always be "Figure 1" since it's the first
# figure created by this process. So do something about it.
f.canvas.set_window_title('My stupid plot number %d' % i)
arr = np.random.uniform(size=(50, 50))
plt.imshow(arr)
plt.show()
p = Process(None, do_something)
processes.append(p) # May want to do other things with objects
p.start()
if __name__ == "__main__":
demo()
@shz224
Copy link

shz224 commented Aug 5, 2021

I encountered an error:

Traceback (most recent call last):
  File ".\demo.py", line 30, in <module>
    demo()
  File ".\demo.py", line 27, in demo
    p.start()
  File "E:\Anaconda3\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "E:\Anaconda3\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "E:\Anaconda3\lib\multiprocessing\context.py", line 326, in _Popen
    return Popen(process_obj)
  File "E:\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "E:\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'demo.<locals>.do_something'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment