当前位置: 首页>>代码示例>>Python>>正文


Python synchronize.Semaphore类代码示例

本文整理汇总了Python中multiprocessing.synchronize.Semaphore的典型用法代码示例。如果您正苦于以下问题:Python Semaphore类的具体用法?Python Semaphore怎么用?Python Semaphore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Semaphore类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testCallbackOnApiTimeout

 def testCallbackOnApiTimeout(self):
     eResult = Result()
     self.apiHead.parent.eResult = eResult
     self.ipcHead.connect()
     self.ipcNeck.connect()
     self.mr.returnExact = True
     args = (0, 1, 2, 3)
     kwargs = {"four":4, "five":5}
     api = self.apiHead.parent.method_c(*args, **kwargs)
     cbData = []
     cbReceived = Semaphore(0)
     def cb(tId, data):
         cbData.append((tId, data))
         cbReceived.release()
     #    Make the call asynchronous:
     api.sync = eSync.ASYNCHRONOUS
     api.callback = cb
     api.solicited = True
     api.timeout = 1
     asyncResult = api()
     assert isinstance(asyncResult, iAsyncResult)
     assert asyncResult
     tId = asyncResult.tId()
     assert tId
     #    Now wait for the callback to have been called:
     assert cbReceived.acquire(timeout=5)
     assert cbData[0][0] == tId
     assert isinstance(cbData[0][1], TransactionFailed)
     #    The semaphore should NOT be acquirable:
     try:
         self.ipcHead.getTransactionManager().acquireNew(tId)
     except TypeError, _e:
         assert True
开发者ID:sys-git,项目名称:epyrpc,代码行数:33,代码来源:TestRoundtrip.py

示例2: MyTransport

class MyTransport(iIpcTransport):
    def __init__(self, *args, **kwargs):
        self.dataSent = []
        self.dataSentLock = Semaphore(0)
    def sendData(self, result, transactionId):
        self.dataSent.append((transactionId, result))
        self.dataSentLock.release()
开发者ID:sys-git,项目名称:epyrpc,代码行数:7,代码来源:TestIApi.py

示例3: JoinableQueue

class JoinableQueue(Queue):

    def __init__(self, maxsize=0):
        Queue.__init__(self, maxsize)
        self._unfinished_tasks = Semaphore(0)
        self._cond = Condition()

    def __getstate__(self):
        return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)

    def __setstate__(self, state):
        Queue.__setstate__(self, state[:-2])
        self._cond, self._unfinished_tasks = state[-2:]

    def put(self, item, block=True, timeout=None):
        Queue.put(self, item, block, timeout)
        self._unfinished_tasks.release()

    def task_done(self):
        self._cond.acquire()
        try:
            if not self._unfinished_tasks.acquire(False):
                raise ValueError('task_done() called too many times')
            if self._unfinished_tasks._semlock._is_zero():
                self._cond.notify_all()
        finally:
            self._cond.release()

    def join(self):
        self._cond.acquire()
        try:
            if not self._unfinished_tasks._semlock._is_zero():
                self._cond.wait()
        finally:
            self._cond.release()
开发者ID:mullens,项目名称:khk-lights,代码行数:35,代码来源:queues.py

示例4: __init__

 def __init__(self, n, count):
     self.n = n
     self.count = count
     self.count[...] = 0
     self.mutex = Semaphore(1)
     self.turnstile = Semaphore(0)
     self.turnstile2 = Semaphore(0)
开发者ID:StevenLOL,项目名称:sharedmem,代码行数:7,代码来源:parallel.py

示例5: _startQReader

 def _startQReader(self):
     if self.qReader==None:
         self.terminateQReader = False
         startMutex = Semaphore(0)
         self.qReader = threading.Thread(target=self.run, args=[startMutex, self._getLogger("QReader.thread", self.loggingLevel)])
         self.qReader.setName("qReader")
         self.qReader.setDaemon(True)
         self.qReader.start()
         startMutex.acquire()
开发者ID:sys-git,项目名称:PyRQ,代码行数:9,代码来源:RRQDebugger.py

示例6: ServerSink

class ServerSink(iMockDebuggerSink):
    def __init__(self, peerName, theTime, details, quiet):
        self._peerName = peerName
        self._methods = []
        methods = iMockDebuggerSink()._getMethods()
        self._methods = methods
        self._terminate = False
        self._details = details
        self._qw = None
        self._startMutex = Semaphore(0)
        self._q = Queue()
        self.quiet= quiet
        self._marshaller = MarshallerFactory.get(MarshallerFactory.DEFAULT, quiet=quiet)
        self._qw = QueueWriter(target=details, autoConnect=True, marshaller=self._marshaller, quiet=quiet)
        self._qw.start()
        self.thread = None
    def start(self):
        t = threading.Thread(target=self.run, args=[self._startMutex])
        t.setName("ServerSink.%(P)s"%{"P":self._peerName})
        t.setDaemon(True)
        self.thread = t
        self.thread.start()
        return "server.sink.started"
    def close(self):
        self._terminate = True
        try:    self.thread.join()
        except: pass
        try:    self._qw.close()
        except: pass
        try:    self._q.close()
        except: pass
        return "server.sink.closed"
    def waitUntilRunning(self, block=True, timeout=None):
        self._startMutex.acquire(block=block, timeout=timeout)
        return self
    def __getattribute__(self, name):
        if name in object.__getattribute__(self, "_methods"):
            q = self._q
            def wrapper(self, *args, **kwargs):
                ServerSink._testPickleability((name, args, kwargs))
                q.put((name, args, kwargs))
            return wrapper
        return object.__getattribute__(self, name)
    def run(self, startMutex):
        startMutex.release()
        while self._terminate==False:
            try:
                data = self._q.get(block=True, timeout=1)
            except Empty:   pass
            else:
                ServerSink._testPickleability(data)
                try:
                    self._qw.put(data, block=True, timeout=10)
                except Exception, _e:
                    break
开发者ID:sys-git,项目名称:PyRQ,代码行数:55,代码来源:ServerSink.py

示例7: MyTransactionManager

class MyTransactionManager(TransactionManager):
    def __init__(self):
        super(MyTransactionManager, self).__init__(StandardTransactionGenerator())
        self.lastTid = None
        self.sem = Semaphore(0)
        self.result = []
    def next(self):
        self.lastTid = super(MyTransactionManager, self).next()
        return self.lastTid
    def release(self, tId, result):
        self.sem.release()
        self.result.append((tId, result))
        super(MyTransactionManager, self).release(tId, result)
开发者ID:sys-git,项目名称:epyrpc,代码行数:13,代码来源:TestApiAction.py

示例8: JoinableQueue

class JoinableQueue(Queue):

    def __init__(self, maxsize = 0):
        Queue.__init__(self, maxsize)
        self._unfinished_tasks = Semaphore(0)
        self._cond = Condition()

    def __getstate__(self):
        return Queue.__getstate__(self) + (self._cond, self._unfinished_tasks)

    def __setstate__(self, state):
        Queue.__setstate__(self, state[:-2])
        self._cond, self._unfinished_tasks = state[-2:]

    def put(self, obj, block = True, timeout = None):
        if not not self._closed:
            raise AssertionError
            raise self._sem.acquire(block, timeout) or Full
        self._notempty.acquire()
        self._cond.acquire()
        try:
            if self._thread is None:
                self._start_thread()
            self._buffer.append(obj)
            self._unfinished_tasks.release()
            self._notempty.notify()
        finally:
            self._cond.release()
            self._notempty.release()

        return

    def task_done(self):
        self._cond.acquire()
        try:
            if not self._unfinished_tasks.acquire(False):
                raise ValueError('task_done() called too many times')
            if self._unfinished_tasks._semlock._is_zero():
                self._cond.notify_all()
        finally:
            self._cond.release()

    def join(self):
        self._cond.acquire()
        try:
            if not self._unfinished_tasks._semlock._is_zero():
                self._cond.wait()
        finally:
            self._cond.release()
开发者ID:webiumsk,项目名称:WOT-0.9.15.1,代码行数:49,代码来源:queues.py

示例9: Item

    class Item(object):
        def __init__(self, i):
            self._i = i
            self._sem = Semaphore(0)
            self._result = []

        def acquire(self, timeout=None):
            return self._sem.acquire(block=True, timeout=timeout)

        def result(self, result):
            self._result.append(result)
            #    Now the result is complete, release it:
            self._sem.release()

        def getResult(self):
            return self._result.pop(0)
开发者ID:sys-git,项目名称:stb-rebooter,代码行数:16,代码来源:Synchroniser.py

示例10: _start

 def _start(self):
     with self._startLock:
         self._synchroniser.checkDestroyed()
         if self._process!=None:
             return
         try:
             sem = Semaphore(0)
             self._process = bootstrapSshProcess(self._target, self._qRx, self._qTx, sem)
             sem.acquire()
             #    Now start the read thread:
             t = threading.Thread(target=self._rx)
             t.setName("SSHRebooter_%(T)s"%{"T":self._target.uId()})
             t.setDaemon(True)
             t.start()
             self._threads["rx"] = t
         except Exception, _e:
             #    Cleanup.
             self.terminate()
开发者ID:sys-git,项目名称:stb-rebooter,代码行数:18,代码来源:SshRebooter.py

示例11: SshClientFactory

class SshClientFactory(object):
    DEFAULT_PORT = 22
    def __init__(self, host, port, username, password, dataQ=None, exitCmd="exit", sshType=SshType.STB, postConnectionTimeout=0):
        self.username = username
        self.password = password
        self.exitCmd = exitCmd
        self.postConnectionTimeout = postConnectionTimeout
        self.host = host
        self.sshType = sshType
        if port==None:
            port = SshClientFactory.DEFAULT_PORT
        self.port = int(port)
        if dataQ==None:
            def dummyPut(*args, **kwargs):
                sys.stderr.write("SshClientFactory consumed data!\n")
            dataQ = dummyPut
        self.dataQ = dataQ
        #
        self.isClosing = False
        self.lastSendTime = time.time()
        #
        self.connection = None
        self.run()
    def run(self):
        sys.stderr.write("SshClientFactory running!\n")
        self.factory = SSHFactory(self)
        self.sem = Semaphore(0)
        def _connectLater():
            sys.stderr.write("SshClientFactory connecting asynchronously\n")
            reactor.connectTCP(self.host, self.port, self.factory)      #@UndefinedVariable
            sys.stderr.write("SshClientFactory connected\n")
        threads.blockingCallFromThread(reactor, _connectLater)          #@UndefinedVariable
        self.sem.acquire()
    def send(self, data, tId, timeout=None):
        client = ClientData(self, cmd=data, tId=tId)
        reactor.callFromThread(self.connection.openAChannel, client)       #@UndefinedVariable
    def terminate(self):
        self.isClosing = True
        if reactor.running:                  #@UndefinedVariable
            reactor.stop()                  #@UndefinedVariable
    def isOk(self):
        return reactor.running          #@UndefinedVariable
    def serviceStarted(self):
        self.sem.release()
开发者ID:sys-git,项目名称:stb-rebooter,代码行数:44,代码来源:SshClient.py

示例12: __init__

 def __init__(self, q, parent):
     super(ApiAsyncWorker, self).__init__()
     self._q = q
     self._parent = parent
     self._name = "ApiAsyncWorker_%(C)s" % {"C":ApiAsyncWorker.ID.next()}
     self._logger = LogManager().getLogger(self._name)
     self.setDaemon(True)
     self.setName(self._name)
     self._cancel = False
     self._runLock = Semaphore(0)
开发者ID:sys-git,项目名称:epyrpc,代码行数:10,代码来源:ApiBase.py

示例13: run

 def run(self):
     sys.stderr.write("SshClientFactory running!\n")
     self.factory = SSHFactory(self)
     self.sem = Semaphore(0)
     def _connectLater():
         sys.stderr.write("SshClientFactory connecting asynchronously\n")
         reactor.connectTCP(self.host, self.port, self.factory)      #@UndefinedVariable
         sys.stderr.write("SshClientFactory connected\n")
     threads.blockingCallFromThread(reactor, _connectLater)          #@UndefinedVariable
     self.sem.acquire()
开发者ID:sys-git,项目名称:stb-rebooter,代码行数:10,代码来源:SshClient.py

示例14: __init__

 def __init__(self, peerName, theTime, filename, quiet):
     self._peerName = peerName
     self._fp = open(filename, "w")
     self.fp.write("File debugger started at: %(T)s for client: %(C)s"%{"T":theTime, "C":peerName})
     self.fp.flush()
     self._methods = []
     methods = iMockDebuggerSink()._getMethods()
     self._methods = methods
     self._terminate = False
     self.quiet=  quiet
     self._startMutex = Semaphore(0)
     self._q = Queue()
     self.thread = None
开发者ID:sys-git,项目名称:PyRQ,代码行数:13,代码来源:FileSink.py

示例15: ApiAsyncWorker

class ApiAsyncWorker(threading.Thread):
    ID = itertools.count(1)
    @staticmethod
    def startAll(workers=[]):
        for worker in workers:
            worker.start()
        for worker in workers:
            worker.waitUntilRunning()
    @staticmethod
    def create(q, parent, start=False):
        worker = ApiAsyncWorker(q, parent)
        if start:
            ApiAsyncWorker.startAll([worker])
        return worker
    def __init__(self, q, parent):
        super(ApiAsyncWorker, self).__init__()
        self._q = q
        self._parent = parent
        self._name = "ApiAsyncWorker_%(C)s" % {"C":ApiAsyncWorker.ID.next()}
        self._logger = LogManager().getLogger(self._name)
        self.setDaemon(True)
        self.setName(self._name)
        self._cancel = False
        self._runLock = Semaphore(0)
    def stop(self):
        self._cancel = True
    def waitUntilRunning(self, timeout=None):
        # Wait until we become running:
        return self._runLock.acquire(block=True, timeout=timeout)
    def run(self):
        self._logger.debug("Thread running...")
        self._runLock.release()
        #    Now do the work:
        try:
            self._work()
        except EOFError, _e:
            self._logger.warn("EOF in thread...")
        except Exception, _e:
            self._logger.exception("Unhandled error in thread:")
开发者ID:sys-git,项目名称:epyrpc,代码行数:39,代码来源:ApiBase.py


注:本文中的multiprocessing.synchronize.Semaphore类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。