python - cat blocking when called via subprocess.Popen() -


im having unexpected behaviour of linux cat when called via subprocess.popen().

the python script structured such:

import os, subprocess  def _degrade_child_rights(user_uid, user_gid):     def result():         os.setgid(user_gid)         os.setegid(user_gid)         os.setuid(user_uid)         os.seteuid(user_uid)     return result  child = subprocess.popen("cat /home/myuser/myfolder/screenlog.0",  preexec_fn=_degrade_child_rights(0, 0), shell=true,  stdout=subprocess.pipe, stderr=subprocess.stdout) 

when check executed shell-command ps aux | grep cat shows me python run shell-command.

> ps aux | grep cat root     21236  0.0  0.0   6564   780 pts/1    s    20:49   0:00 /bin/sh -c cat /home/myuser/myfolder/screenlog.0 root     21237  0.0  0.0  11056   732 pts/1    s    20:49   0:00 cat /home/myuser/myfolder/screenlog.0 root     21476  0.0  0.0  15800   936 pts/1    s+   20:52   0:00 grep --color=auto cat 

however, cat command never finishes.

i outsourced cat $file command bash-script. bash executes cat-call, blocks.

when manually execute cat $file runs expected, not existing eof @ end of file impossible.

i think, '/bin/sh -c' added popen messes somehow correct execution of cat $file.

can somehow prevent this?

you might want try communicate method popen object:

popen.communicate(input=none) interact process: send data stdin. read data stdout , stderr, until end-of-file reached. wait process terminate. optional input argument should string sent child process, or none, if no data should sent child.

communicate() returns tuple (stdoutdata, stderrdata).

note if want send data process’s stdin, need create popen object stdin=pipe. similarly, other none in result tuple, need give stdout=pipe and/or stderr=pipe too.

note data read buffered in memory, not use method if data size large or unlimited.

there more info in subprocess python doc


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -