How to run/interact with a Golang executable in Python?
<p>I have a command-line Golang executable on Windows named <code>cnki-downloader.exe</code> (open-sourced here: <a href="https://github.com/amyhaber/cnki-downloader" rel="nofollow">https://github.com/amyhaber/cnki-downloader</a>). I want to run this executable in Python, and interact with it (get its output, then input something, then get output, and so on)</p>

<p>It's a command-line program, so I thought it's the same as a normal Windows command-line program built by MSVC. My code is like this:</p>

<pre><code># coding=gbk

from subprocess import Popen, PIPE

p = Popen(["cnki-downloader.exe"], stdin=PIPE, stdout=PIPE)
#p = Popen(["WlanHelper.exe"], stdin=PIPE, stdout=PIPE )

p.stdin.write( 'XXXXXX\n' )
result1 = p.stdout.read() # &lt;---- we never return here
print result1

p.stdin.write( '1\n' )
result2 = p.stdout.read()
print result2
</code></pre>

<p>My Python code halts at the <code>Popen</code> call with <code>cnki-downloader.exe</code> parameter. Then I tried a C command-line program built by MSVC (named <code>WlanHelper.exe</code>), it runs well. My script can get the output from the exe.</p>

<p>So I suspect that the command-line mechanism of the Golang executable is different from a native C/C++ program, and it's difficult for other languages (like Python) to call and interact with it.</p>

<p>So I want to know <strong>how to interact with a Golang executable on Windows in other languages like Python?</strong>. If this is impossible, I can also consider modifying the Golang program's source code (because it's open source). But I hope I won't go that step. Thanks!</p>

<hr>

<p><strong>NOTE:</strong></p>

<p>If possible, I want to call this Golang executable directly, instead of modifying it into a library and let Python import it. If I have to modify the Golang into a library, why not just remove the interactive manner and make everything into command-line parameters? There's no need to bother writing a Golang library. So please let's assume the Golang program is closed-sourced. I don't think there's no way for Python to call a command-line Golang program. If this is the case, then I think Golang <strong>REALLY</strong> needs to be improved in the interoperability with other languages.</p>

<p>Looking at the <a href="https://docs.python.org/2/library/subprocess.html#popen-objects" rel="nofollow">documentation</a> for the subprocess module you are using, it seems they have a warning about deadlocks using <code>stdout.read()</code> and friends:</p>

<blockquote>
  <p>Warning This will deadlock when using stdout=PIPE and/or stderr=PIPE
  and the child process generates enough output to a pipe such that it
  blocks waiting for the OS pipe buffer to accept more data. Use
  communicate() to avoid that.</p>
</blockquote>

<p>So, maybe try using <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">communicate</a> instead?</p>

python windows go command-line