How to Avoid Deadlock when Calling External Command from Python

Use pipes from multiple threads for bidirectional communication.

“The truth will set you free, but first it will piss you off.” ― Joe Klaas, Twelve Steps to Happiness

1. Introduction

Invoking an external process from python and interacting with it can be quite tricky. This is especially true if the interaction is duplex i.e. involving both reading and writing to it. Such an interaction can cause deadlocks since both processes can end up waiting for output from the other. One way to avoid such deadlocks is to separate the reader and writer parts to different threads. We demonstrate such an approach in this article.

Continue reading “How to Avoid Deadlock when Calling External Command from Python”

Run External Command in Python Using Popen

Use subprocess.Popen for more control over the child process

“The world is a book and those who do not travel read only one page.” ― Augustine of Hippo

1. Introduction

In a previous article, we looked at calling an external command in python using That method allows you to accomplish several tasks during the invocation:

Continue reading “Run External Command in Python Using Popen”

How to Call an External Command in Python

Learn about the various aspects of the subprocess module.

“I hate quotations. Tell me what you know.” ― Ralph Waldo Emerson, The Essays of Ralph Waldo Emerson

1. Introduction

Do you want to run an external command from inside python? Maybe you want to startup a daemon process (no input or output), or perhaps invoke a command and capture its output?

Continue reading “How to Call an External Command in Python”

Java Process Example Part 2

When reading and writing to a child process, it is necessary to execute the read and write blocks in separate threads to avoid deadlock.

1. Introduction

In the previous part of this Java Process Guide, we looked at how to read from and write to a child process. While executing the read and write blocks in a single thread works for simple cases, we should run these in separate threads to avoid deadlocks.

Continue reading “Java Process Example Part 2”

Java Process Example

Startup a native process from Java using Runtime. Read the output from the process and write some data to it. After you are done, terminate the process.

1. Introduction

Have you ever run into a situation where you need to execute an OS command from inside java and read its output? You can use the Process class to do so, but there are some caveats to consider.

Continue reading “Java Process Example”