Skip to content

Commit

Permalink
multiprocessing.Lock access to ManagerLog.write
Browse files Browse the repository at this point in the history
rational: otherwise sub-processes sometimes write over each other
  • Loading branch information
lirazsiri committed Dec 4, 2012
1 parent c1f7a8c commit 000f73b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cloudtask/session.py
Expand Up @@ -27,6 +27,8 @@

import re

from multiprocessing import Lock

def makedirs(path, mode=0750):
try:
os.makedirs(path, mode)
Expand Down Expand Up @@ -141,11 +143,16 @@ def __getattr__(self, attr):
class ManagerLog:
def __init__(self, path):
self.fh = file(path, "a", 1)
self.lock = Lock()

def write(self, buf):
self.fh.write(buf)
sys.stdout.write(buf)
sys.stdout.flush()
self.lock.acquire()
try:
self.fh.write(buf)
sys.stdout.write(buf)
sys.stdout.flush()
finally:
self.lock.release()

def __getattr__(self, attr):
return getattr(self.fh, attr)
Expand Down

0 comments on commit 000f73b

Please sign in to comment.