-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from iamdefinitelyahuman/solc-paths
Solc paths
- Loading branch information
Showing
5 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import os | ||
from pathlib import Path | ||
import sys | ||
import tempfile | ||
import threading | ||
|
||
if sys.platform == "win32": | ||
import msvcrt | ||
OPEN_MODE = os.O_RDWR | os.O_CREAT | os.O_TRUNC | ||
else: | ||
import fcntl | ||
NON_BLOCKING = fcntl.LOCK_EX | fcntl.LOCK_NB | ||
BLOCKING = fcntl.LOCK_EX | ||
|
||
_locks = {} | ||
_base_lock = threading.Lock() | ||
|
||
|
||
def get_process_lock(lock_id): | ||
with _base_lock: | ||
if lock_id not in _locks: | ||
if sys.platform == "win32": | ||
_locks[lock_id] = WindowsLock(lock_id) | ||
else: | ||
_locks[lock_id] = UnixLock(lock_id) | ||
return _locks[lock_id] | ||
|
||
|
||
class _ProcessLock: | ||
|
||
def __init__(self, lock_id): | ||
self._lock = threading.Lock() | ||
self._lock_path = Path(tempfile.gettempdir()).joinpath(f'.solcx-lock-{lock_id}') | ||
self._lock_file = self._lock_path.open('w') | ||
|
||
def wait(self): | ||
self.acquire(True) | ||
self.release() | ||
|
||
|
||
class UnixLock(_ProcessLock): | ||
|
||
def acquire(self, blocking): | ||
if not self._lock.acquire(blocking): | ||
return False | ||
try: | ||
fcntl.flock(self._lock_file, BLOCKING if blocking else NON_BLOCKING) | ||
except BlockingIOError: | ||
self._lock.release() | ||
return False | ||
return True | ||
|
||
def release(self): | ||
fcntl.flock(self._lock_file, fcntl.LOCK_UN) | ||
self._lock.release() | ||
|
||
|
||
class WindowsLock(_ProcessLock): | ||
|
||
def acquire(self, blocking): | ||
if not self._lock.acquire(blocking): | ||
return False | ||
while True: | ||
try: | ||
fd = os.open(self._lock_path, OPEN_MODE) | ||
msvcrt.locking(fd, msvcrt.LK_LOCK if blocking else msvcrt.LK_NBLCK, 1) | ||
self._fd = fd | ||
return True | ||
except OSError: | ||
if not blocking: | ||
self._lock.release() | ||
return False | ||
|
||
def release(self): | ||
msvcrt.locking(self._fd, msvcrt.LK_UNLCK, 1) | ||
self._lock.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/python3 | ||
|
||
import multiprocessing as mp | ||
import threading | ||
|
||
import solcx | ||
|
||
|
||
class ThreadWrap: | ||
|
||
def __init__(self, fn, *args, **kwargs): | ||
self.exc = None | ||
self.t = threading.Thread(target=self.wrap, args=(fn,)+args, kwargs=kwargs) | ||
self.t.start() | ||
|
||
def wrap(self, fn, *args, **kwargs): | ||
try: | ||
fn(*args, **kwargs) | ||
except Exception as e: | ||
self.exc = e | ||
|
||
def join(self): | ||
self.t.join() | ||
if self.exc is not None: | ||
raise self.exc | ||
|
||
|
||
def test_threadlock(nosolc): | ||
threads = [ThreadWrap(solcx.install_solc, '0.5.0') for i in range(4)] | ||
for t in threads: | ||
t.join() | ||
|
||
|
||
def test_processlock(nosolc): | ||
mp.set_start_method('spawn') | ||
threads = [mp.Process(target=solcx.install_solc, args=('0.5.0',),) for i in range(4)] | ||
for t in threads: | ||
t.start() | ||
solcx.install_solc('0.5.0') | ||
for t in threads: | ||
t.join() | ||
assert t.exitcode == 0 |