4

How do I make this a non-blocking call? osd_cat accepts input only as a PIPE which need p.communicate() call making the process to block. Is there any other way to set stdin in Popen?

p = subprocess.Popen(('osd_cat',
                      '-d',
                      '{}'.format(interval)),
                     stdin=subprocess.PIPE)
p.communicate(message)
2
  • Use Python Multiprocessing and put your sub task in an other process. Talk to it using a message queue. Commented Jun 26, 2019 at 16:03
  • Waitaminute, what you're stuck on is how else to feed to stdin? Have you tried p.stdin.write() before asking the question? (Yes, you'll want to do that in a separate thread to prevent blocking in the main process, but this is part of what threads are good for). Commented Jun 26, 2019 at 16:50

1 Answer 1

3

The p.communicate method is a one-shot deal in terms of sending data to the process.

Instead, write directly to p.stdin. If you want to get output, you can read lines from p.stdout. Make sure you pass stdout=subprocess.PIPE to the constructor before attempting to read.

Sign up to request clarification or add additional context in comments.

2 Comments

Why does p.communicate block though, when you pass a string? It could close pipe internally since we are passing a string..right?
@Jean. That's just what it's designed and documented to do. It optionally sends a single string, then waits for the process so close its stdout (usually by terminating). There's no particular overarching reason, except that that's a very common way to use processes. Perhaps it's the choice of name that's confusing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.