Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you only communicate once with a subprocess?

communicate's documentation says:

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

What do you do if you need to send input to a process more than once ? For example, I spawn a process, send it some data, the process does something with that, returns some output, and then I have to send input again? How do I handle that?

like image 588
Geo Avatar asked Jan 24 '23 01:01

Geo


2 Answers

Then you can't use .communicate(). You can either poll the streams, use select or some other way that allows you to listen to FD changes (both gtk and Qt have tools for that, for example).

like image 109
Lukáš Lalinský Avatar answered Jan 30 '23 12:01

Lukáš Lalinský


Take a look at Doug Hellman's Python Module of the Week writeup about subprocess. Search down until you see "repeater.py".

There you will find an example of how to send and receive input/output to a process.

like image 23
unutbu Avatar answered Jan 30 '23 12:01

unutbu