stdout

Interfacing Python with a real-time stdout of a multithreaded-C-program


I’m trying to figure out the problem of retrieve the real-time streamed stdout from a multithreaded-C-program.

I found that Cython can retrieve a single return of a function but how can I handle the output of something like this:

void threaded_function(){
    int i = 0;
    for(;;){
         i += 1;
         print("result: %i",i);
    }
}

I tried with subprocess lib of python but it doesn’t work.



لینک منبع

python 2.7 – How to kill plink.exe when there’s no output from stdout?


Plink.exe does not close when the process is finished. The code got stuck when trying to break the while loop by checking if output == ‘ ‘: break, but still no luck. I’m able to print the live output, but unable to break the loop when it’s completed.

#Establish ssh connection to a server
def _establish_connection(self):
    connection='plink -ssh {}@{} -pw {}'.format(self.user,self.server,self.pw)
    self.sp = Popen(connection,stdin=PIPE,stdout=PIPE) 



#Trying to read live output from stdout and write to file
def _test_create_log(self,_system,_logdir):
     self._establish_connection()
     self.sp.stdin.write(_command)
     timestr = time.strftime("%Y%m%d-%H%M%S")
     dir_to_log = os.path.join(_logdir,_system + '_' + timestr + '.txt')
     with open(dir_to_log,'w+') as myLog:
         while True:
             output = self.sp.stdout.readline()
             output.decode('ASCII')
             if output != '': 
                 myLog.write(output)
                 print(output.strip())
             else: #Code does not reach here, plink not killed.
                 self.sp.kill()
                 break



لینک منبع