MCPcopy Create free account
hub / github.com/ActiveState/code / __init__

Method __init__

recipes/Python/578998_SystemMutex/recipe-578998.py:43–83  ·  view source on GitHub ↗
(self, mutex_name)

Source from the content-addressed store, hash-verified

41 class SystemMutex(object):
42
43 def __init__(self, mutex_name):
44 check_valid_mutex_name(mutex_name)
45 filename = os.path.join(tempfile.gettempdir(), mutex_name)
46 try:
47 os.unlink(filename)
48 except:
49 pass
50 try:
51 handle = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_RDWR)
52 try:
53 try:
54 pid = str(os.getpid())
55 except:
56 pid = 'unable to get pid'
57 os.write(handle, pid)
58 except:
59 pass # Ignore this as it's pretty much optional
60 except:
61 self._release_mutex = NULL
62 self._acquired = False
63 else:
64 def release_mutex(*args, **kwargs):
65 # Note: can't use self here!
66 if not getattr(release_mutex, 'called', False):
67 release_mutex.called = True
68 try:
69 os.close(handle)
70 except:
71 traceback.print_exc()
72 try:
73 # Removing is optional as we'll try to remove on startup anyways (but
74 # let's do it to keep the filesystem cleaner).
75 os.unlink(filename)
76 except:
77 pass
78
79 # Don't use __del__: this approach doesn't have as many pitfalls.
80 self._ref = weakref.ref(self, release_mutex)
81
82 self._release_mutex = release_mutex
83 self._acquired = True
84
85 def get_mutex_aquired(self):
86 return self._acquired

Callers

nothing calls this directly

Calls 8

check_valid_mutex_nameFunction · 0.85
strFunction · 0.85
unlinkMethod · 0.80
openFunction · 0.50
joinMethod · 0.45
openMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected