0

I have a code which spawns at the max 4 processes at a go. It looks for any news jobs submitted and if it exists it runs the python code

for index,row in enumerate(rows):
    if index < 4:
        dirs=row[0]
        dirName=os.path.join(homeFolder,dirs)
        logFile=os.path.join(dirName,(dirs+".log"))
        proc=subprocess.Popen(["python","test.py",dirs],stdout=open(logFile,'w'))

I have few questions:

  1. When I try to write the output or errors in log file it does not write into the file until the process finishes.Is it possible to write the output in the file as the process runs as this will help to know at what stage it is running.
  2. When one process finishes, I want the next job in the queue to be run rather than waiting for all child processes to finish and then the daemon starts any new.

Any help will be appreciated.Thanks!

4
  • 1
    Please take a bit of care with your formatting. It's not hard to do and you've got a preview right there before you post. Commented Aug 25, 2011 at 10:45
  • Stop editing and breaking it, @shash! Commented Aug 25, 2011 at 10:51
  • I am sorry, I will be careful in future. Thanks for correcting me. Commented Aug 25, 2011 at 10:56
  • @shash, make the world a better place and follow PEP8 ;-) Commented Aug 25, 2011 at 11:42

2 Answers 2

1

For 2. you can take a look at http://docs.python.org/library/multiprocessing.html

Sign up to request clarification or add additional context in comments.

Comments

1

Concerning point 1, try to adjust the buffering used for the log file:

open(logFile,'w', 1) # line-buffered (writes to the file after each logged line)
open(logFile,'w', 0) # unbuffered (should immediately write to the file)

If it suits your need, you should choose line-buffered instead of unbuffered.

Concerning your general problem, as @Tichodroma suggests, you should have a try with Python's multiprocessing module.

1 Comment

I tried both the options but it does not work. It buffers everything and then write it to the file at the end. proc=subprocess.Popen(["python","test.py",dirs],stdout=open(logFile,’w’,0))proc=subprocess.Popen(["python","test.py",dirs],stdout=open(logFile,’w’,1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.