开发者

FileIO out of order when using subprocesses in Python

开发者 https://www.devze.com 2023-01-03 16:30 出处:网络
I am trying to generate a log file with information in order. This is what I have: class ExecThread(threading.Thread):

I am trying to generate a log file with information in order. This is what I have:

class ExecThread(threading.Thread):
 def __init__(self, command):
  self.command = command
  self._lock = threading.Lock()
  threading.Thread.__init__ ( self )

 def run ( self ):
  self._lock.acquire()
  sys.stdout.write(''.join(["Executing: ",self.command,'\n']))
  log_file.write(''.join([self.command,'\n']))
  os.system(self.command)
  self._lock.release()

for ive in locate('*.ive', root_dir):
  command = "osgconv"
  command = ''.join([command,' ',"-O OutputTextureFiles",' ', infile,' ', outfile,' ',"2>&1"])

  conv_osg_thread = ExecThread(command)
  conv_osg_thread.start()
  conv_osg_thread.join()

The command I am executing has this redirection at the end: "2>&1" When I run this I get the output of the subprocess before the message "Executing blah", which is listed first! I though开发者_JAVA技巧t the lock() would fix it, but no.

Please help, I would really appreciate if someone could point out my error.


I/O is buffered by default. Try sys.stdout.flush() after sys.stdout.write().

0

精彩评论

暂无评论...
验证码 换一张
取 消