Python threading - return control to the terminal while maintaining the frame

To summarize: I want to open a frame in Python, be able to work with this frame, and also be able to continue to use this terminal.

Basically, I would like to emulate some Matlab behavior in Python. In Matlab you can:

x=0:10; y=0:10; plot(x,y) 

and the graph will appear, it will be interactive, and you will also get access to the terminal to make a difference and do other work.

I realized that in Python, if I sliced ​​correctly, I could do the same. However, the code below controls the terminal.

 from threading import Thread from matplotlib import pyplot as plt class ThreadFrame(Thread): def __init__(self): Thread.__init__(self) def setup(self): my_plot = plt.plot(range(0,10), range(0,10)) fig = plt.gcf() ax = plt.gca() my_thread = ThreadFrame() my_thread.start() my_thread.setup() plt.show() 

Is there something I have to do differently with threads? Or is there another way to do this?

+1
source share
2 answers

matplotlib enjoys using plt.show (). To get the behavior you need, check out the docs when using matplotlib interactively . It's nice that no thread is required.

(In your solution, the plot is prepared in the background, the background thread ends, and your front threads remain in plt.show)

+3
source

jtniehof the answer is good. For an alternative, you can look at pysvr inside the Python source distribution. It allows you to connect to a running Python process and get the REPL this way.

0
source

Source: https://habr.com/ru/post/1479013/


All Articles