Skip to content

Commit c3b671d

Browse files
committed
Refactored implementation
1 parent f6d8838 commit c3b671d

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

mbed/mbed.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from itertools import chain, repeat
4646
import time
4747
import zipfile
48-
import argparse
48+
from random import randint
4949

5050

5151
# Application version
@@ -156,7 +156,7 @@ def log(msg, is_error=False):
156156
sys.stderr.write(msg) if is_error else sys.stdout.write(msg)
157157

158158
def message(msg):
159-
return "[mbed] %s\n" % msg
159+
return "[mbed-%s] %s\n" % (os.getpid(), msg)
160160

161161
def info(msg, level=1):
162162
if level <= 0 or verbose:
@@ -1345,6 +1345,7 @@ def cache_lock(self, url):
13451345
cpath = self.url2cachedir(url)
13461346
if cpath:
13471347
lock_file = os.path.join(cpath, '.lock')
1348+
13481349
if not os.path.isdir(cpath):
13491350
os.makedirs(cpath)
13501351

@@ -1353,35 +1354,29 @@ def cache_lock(self, url):
13531354
for i in range(300):
13541355
if i:
13551356
time.sleep(1)
1356-
if os.path.isfile(lock_file):
1357-
try:
1358-
# lock file exists, but we need to check pid as well in case the process died
1357+
1358+
try:
1359+
# lock file exists, but we need to check pid as well in case the process died
1360+
if os.path.isfile(lock_file):
13591361
with open(lock_file, 'r', 0) as f:
1360-
pid = f.read(8)
1361-
if pid and int(pid) != os.getpid():
1362-
if self.pid_exists(pid):
1363-
info("Cache lock file exists and process %s is alive." % pid)
1364-
continue
1362+
pid = int(f.read(8))
1363+
if pid != os.getpid() and self.pid_exists(pid):
1364+
info("Cache lock file exists and process %s is alive." % pid)
13651365
else:
1366-
info("Cache lock file exists, but process is dead. Cleaning up")
1366+
info("Cache lock file exists, but %s is dead. Cleaning up" % pid)
13671367
os.remove(lock_file)
1368-
continue
1369-
except (IOError, OSError):
13701368
continue
1371-
pass
1372-
1373-
can_lock = True
1374-
break
13751369

1376-
if can_lock:
1377-
try:
13781370
with open(lock_file, 'wb', 0) as f:
1379-
info("Writing cache lock file for pid %s" % os.getpid())
1371+
info("Writing cache lock file %s for pid %s" % (lock_file, os.getpid()))
13801372
f.write(str(os.getpid()))
13811373
f.flush()
13821374
os.fsync(f)
1375+
break
13831376
except (IOError, OSError):
1384-
error("Unable to lock cache dir \"%s\"" % (cpath))
1377+
error("OS error occurred")
1378+
except Exception as e:
1379+
error("Weird error occurred")
13851380

13861381
else:
13871382
error("Exceeded 5 minutes limit while waiting for other process to finish caching")

0 commit comments

Comments
 (0)