Passing data between separately running Python scripts

前端 未结 3 1935
眼角桃花
眼角桃花 2020-12-01 11:06

If I have a python script running (with full Tkinter GUI and everything) and I want to pass the live data it is gathering (stored internally in arrays and such) to another p

3条回答
  •  有刺的猬
    2020-12-01 11:47

    you can use multiprocessing module to implement a Pipe between the two modules. Then you can start one of the modules as a Process and use the Pipe to communicate with it. The best part about using pipes is you can also pass python objects like dict,list through it.

    Ex: mp2.py:

    from multiprocessing import Process,Queue,Pipe
    from mp1 import f
    
    if __name__ == '__main__':
        parent_conn,child_conn = Pipe()
        p = Process(target=f, args=(child_conn,))
        p.start()
        print(parent_conn.recv())   # prints "Hello"
    

    mp1.py:

    from multiprocessing import Process,Pipe
    
    def f(child_conn):
        msg = "Hello"
        child_conn.send(msg)
        child_conn.close()
    

提交回复
热议问题