def __init__(self): """ Args: chain (obj): Network parameters to associate with the wallet. Should probably move this to the account level. """ # The path to the filesystem location of the encrypted wallet file. self.path = None # The AccountManager that holds all account information. acctManager is # saved with the encrypted wallet file. self.acctManager = None self.selectedAccount = None self.openAccount = None # The fileKey is a hash generated with the user's password as an input. # The fileKey hash is used to AES encrypt and decrypt the wallet file. self.fileKey = None # An object implementing the BlockChain API. Eventually should be move # from wallet in favor of a common interface that wraps a full, spv, or # light node. self.blockchain = None # The best block. self.users = 0 # A user provided callbacks for certain events. self.signals = None self.mtx = Mutex() self.version = None
def __init__(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.thread_recv = Thread(target=self.__rcv_thread__) self.mutex_values = Mutex() self.values = self.__init_values__() self.OK = False return
def __init__(self, ip_, port_, sock_=None): self.ip = ip_ self.port = port_ self.thread_rcv = Thread(target=self.__receive__) self.mutex_values = Mutex() if sock_== None: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock_ TcasConnection.count += 1 self.id = TcasConnection.count self.values = self.__init_values__() self.valid = True return
import time from concurrent.futures import ThreadPoolExecutor from threading import Lock as Mutex m = Mutex() num = 1 def run(workload): global num, m # critical section` m.acquire() if num == 1: time.sleep(workload) num = num + 1 m.release() return num if __name__ == '__main__': workers = 5` workload = 0 with ThreadPoolExecutor(max_workers=workers) as executor: res = executor.map(run, [workload] * 5) print(list(res))
class Lock(object): mutex = Mutex() def __init__(self, path): self.depth = 0 self.path = path self.lockdir = None self.blocking = None lock_dir, fn = os.path.split(self.path) try: if not os.path.exists(lock_dir): os.makedirs(lock_dir) self.lockdir = lock_dir except Exception: self.lockdir = None def acquire(self, blocking=None): """Behaviour here is modeled after threading.RLock.acquire. If 'blocking' is False, we return True if we didn't need to block and we acquired the lock. We return False if couldn't acquire the lock and would have had to wait and block. if 'blocking' is None, we return None when we acquire the lock, otherwise block until we do. if 'blocking' is True, we behave the same as with blocking=None, except we return True. """ if self.lockdir is None: return f = LockFile(self.path) try: try: while True: f.open() f.getpid() if f.mypid(): self.P() if blocking is not None: return True return if f.valid(): f.close() # Note: blocking has three meanings for # None, True, False, so 'not blocking' != 'blocking == False' if blocking is False: return False time.sleep(LOCK_WAIT_DURATION) else: break self.P() f.setpid() except OSError, e: log.exception(e) print "could not create lock" finally: f.close() # if no blocking arg is passed, return nothing/None if blocking is not None: return True return None def release(self): if self.lockdir is None: return if not self.acquired(): return self.V() if self.acquired(): return f = LockFile(self.path) try: f.open() f.delete() finally: f.close() def acquired(self): if self.lockdir is None: return mutex = self.mutex mutex.acquire() try: return (self.depth > 0) finally: mutex.release() # P def wait(self): mutex = self.mutex mutex.acquire() try: self.depth += 1 finally: mutex.release() return self P = wait # V def signal(self): mutex = self.mutex mutex.acquire() try: if self.acquired(): self.depth -= 1 finally: mutex.release() V = signal def __del__(self): try: self.release() except Exception: pass
class Global: SAMPLING_FREQ = 15 SAMPLING_TIME = 1 / SAMPLING_FREQ MY_PORT = "8000" MUTEX_CONN = Mutex() FINISH = False
class Lock: mutex = Mutex() def __init__(self, path): self.depth = 0 self.path = path self.lockdir = None lock_dir, fn = os.path.split(self.path) try: if not os.path.exists(lock_dir): os.makedirs(lock_dir) self.lockdir = lock_dir except: self.lockdir = None def acquire(self): if self.lockdir is None: return f = LockFile(self.path) try: try: while True: f.open() f.getpid() if f.mypid(): self.P() return if f.valid(): f.close() time.sleep(0.5) else: break self.P() f.setpid() except OSError: print "could not create lock" finally: f.close() def release(self): if self.lockdir is None: return if not self.acquired(): return self.V() if self.acquired(): return f = LockFile(self.path) try: f.open() f.delete() finally: f.close() def acquired(self): if self.lockdir is None: return mutex = self.mutex mutex.acquire() try: return (self.depth > 0) finally: mutex.release() def P(self): mutex = self.mutex mutex.acquire() try: self.depth += 1 finally: mutex.release() return self def V(self): mutex = self.mutex mutex.acquire() try: if self.acquired(): self.depth -= 1 finally: mutex.release() def __del__(self): try: self.release() except: pass