smoquet
smoquet

Reputation: 371

Python's subrocess module hangs in PyCharm on self.stdout.read() only when using Chainpoint cli tool commands

Python's subprocess module hangs when calling chp (Chainpoint cli tool) commands. But only when I do this inside PyCharm. Doing the same in a Python shell directly in the terminal works perfectly. Also using other processes works fine in PyCharm. It seems to be the combination between chp and PyCharm that creates this failure.

This is what i try:

outputs_raw = subprocess.check_output(['chp', 'version'])

it eventually hangs at:

stdout = self.stdout.read() in subprocess.py

I looked around for a solution, but all the other "subprocess hangs pycharm" results don't help.

I also tried using readingproc as an alternative, advised here. This gave me an interesting result. It keeps looping in readingproc/core.py:

while self._proc.poll() is None:
    with _unblock_read(self._proc):
        result = self._yield_ready_read()
        self._check_timeouts(chunk_timeout, total_timeout)    
        if result is not None:
            self._update_chunk_time()
            yield result                    

here result is always None, as self._yield_ready_read() keeps returning None so the if statement never passes.

This is what the _yield_ready_read function looks like. ( in core.py)

def _yield_ready_read(self):
    stdout = b''
    stderr = b''

    if self._poll_stdout.poll(0):
        stdout = self._read_while(self._proc.stdout)
    if self._poll_stderr.poll(0):
         stderr = self._read_while(self._proc.stderr)

    if len(stdout) > 0 or len(stderr) > 0:
        return ProcessData(stdout, stderr)
    else:
        return None

I am using python 3.7.3

PATH is the same in the working environment and the failing one.

Can someone help me fix this issue? Thanks!

Upvotes: 1

Views: 130

Answers (1)

smoquet
smoquet

Reputation: 371

This fixed it:

from subprocess import Popen, PIPE, STDOUT
proc = Popen(command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
outputs_raw, errs = proc.communicate()

Upvotes: 1

Related Questions