开发者

Python threading- returning control to the terminal while keeping a frame open

开发者 https://www.devze.com 2023-03-03 12:52 出处:网络
To sum up: I want to open a frame in Python, be able to work with that frame, and also be able to continue using that terminal.

To sum up: I want to open a frame in Python, be able to work with that frame, and also be able to continue using that terminal.

Basically, I'd like to mimic some Matlab behavior in Python. In Matlab, you can do:

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

and a plot will come up, be interactive, and you also have access to the terminal to change things and do other work.

I figured that in Python, if I threaded properly, I could do the same thing. However, the code below keeps control of 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.pl开发者_Go百科ot(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 should be doing differently with the threading? Or is there another way to accomplish this?


matplotlib gets funny about using plt.show(). To get the behaviour you want, look at the docs on using matplotlib interactively. The nice thing is, no threading necessary.

(In your solution, the plot is prepared in the background, the background thread terminates, and your foreground threads stays in the plt.show)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消