Note that there are some explanatory texts on larger screens.

plurals
  1. POquestion about pexpect in python
    text
    copied!<p>I tried both pexpect and subprocess.Popen from python to call an external long term background process (this process use socket to communicate with external applications), with following details.</p> <ol> <li><p>subprocess.Popen(launchcmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) This works fine. I do not need to do anything else. However, because I have to get the output immediately, I choose pexpect to avoid the pipe file buffer problem.</p></li> <li><p>obj= pexpect.spawn(launchcmd, timeout=None) after launching external process, I use a separate thread to do "readline" to read the output of the launched process "obj", and everything is ok.</p></li> <li><p>obj= pexpect.spawn(launchcmd, timeout=None) after launching external process, I did nothing further, i.e., just leave it there. Although, by using the "ps -e" command I can find the launched process, but the launched process seems blocked and cannot communicate in sockets with other applications. </p></li> </ol> <p>OK. To be more specific, I put some sample code to formulate my question.</p> <pre><code>import subprocess import pexpect import os t=1 while(True): if(t==1): background_process="./XXX.out" launchcmd = [background_process] #---option 3-------- p=pexpect.spawn(launchcmd, timeout=None) # process launced, problem with socket. #---option 1-------- p=subprocess.Popen(launchcmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # process launced, everything fine t=0 </code></pre> <p>Could anyone tell me what's wrong with the 3rd option? And if it is due to the fact that I did not use a separate thread to manipulate the output, why 1st option works with subprocess.popen? I suspect there is something wrong with pexpect to launch a process using socket, but I am not sure, especially considering option 2 works well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload