0


Here I have two scripts: main.py and sub.py

  • sub.py is a wxpython based app which just show a text (received as a required parameter, for example: sub.py "Hello world!").
  • In main.py, it calls sub.py using subprocess and send a parameter at first call.

And my problem is how to update the corresponding parameter of sub.py in main.py which re-send a new parameter without restart (or recall) sub.py?
Something like these in main.py:

subprocess.Popen('sub.py "Hello, world"')
subprocess.update('sub.py "Hi, world!"')#(just update the parameter without reopen sub.py)<br>

Is this possible? So how to design such programs that communicate each other?
Thank you!!!

good man
  • 339
  • 6
  • 15

1 Answers1

2

You cannot update the parameters that were used to start a program. However, you can use other forms of interprocess communication to do so. An easy way would be to have "sub.py" read its parameters from standard input. Each time it reads a new line, it will display new text.

Hans Then
  • 10,935
  • 3
  • 32
  • 51
  • +1, I think you'd need something like `select` so that the UI thread wouldn't freeze when waiting for stdin. It won't easily work for Windows though, see http://stackoverflow.com/questions/12499523/using-sys-stdin-in-select-select-on-windows?lq=1 – Kos Aug 20 '13 at 11:17
  • Thank you for your suggestion! "stdin/stdout" via subprocess.PIPE is a good way!! – good man Aug 20 '13 at 11:51
  • 1
    You can avoid the UI from freezing by running a separate thread that reads stdin and puts the data into a `Queue` which the main thread can pull information from whenever it pleases. – martineau Aug 20 '13 at 11:52
  • If memory serves me well wxPython already uses a separate event thread for GUI events. But using a queue pattern is good advice anyway. – Hans Then Aug 22 '13 at 09:41