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


Python Semaphore.release方法代码示例

本文整理汇总了Python中eventlet.semaphore.Semaphore.release方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.release方法的具体用法?Python Semaphore.release怎么用?Python Semaphore.release使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在eventlet.semaphore.Semaphore的用法示例。


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

示例1: __init__

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]
class MJPEGProxy:
    def __init__(self, listen_address, connect_address):
        self.log = logging.getLogger('MJPEGProxy')
        self.connect_address = connect_address
        self.listen_address = listen_address
        self.clients = []
        self.connection = None
        self.header = None
        self.sem = Semaphore(1)

    def run(self):
        self.log.info("Starting")
        eventlet.spawn_n(self.proxy)
        self.listen()

    def listen(self):
        server = eventlet.listen(self.listen_address)
        while True:
            connection, address = server.accept()
            self.add_client(connection, address)

    def proxy(self):
        while True:
            eventlet.sleep(0) # sem.release(); sem.acquire() doesn't yield?!
            self.sem.acquire()
            if len(self.clients) == 0:
                if self.connection:
                    self.disconnect()

                self.sem.release()
                eventlet.sleep(0.1)
                continue

            self.sem.release()

            data = ''
            try:
                data = self.connection.recv(1024)
            except:
                self.log.info("Timed out reading data from source.")

            if (len(data) == 0):
                self.log.info("No data recieved from source, forcing reconnect.");
                self.disconnect()
                data = self.connect()

            for client in self.clients:
                try:
                    client.send(data)
                except socket.error, err:
                    self.clients.remove(client)
                    client.close()
                    self.log.info("Client %s disconnected: %s [clients: %s]", client, err, len(self.clients))
开发者ID:Jonty,项目名称:mjpegproxy,代码行数:55,代码来源:mjpegproxy.py

示例2: EntrypointWaiter

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]
class EntrypointWaiter(InjectionProvider):
    """Helper for `entrypoint_waiter`

    Injection to be manually (and temporarily) added to an existing container.
    Takes an entrypoint name, and exposes a `wait` method, which will return
    once the entrypoint has fired.
    """

    def __init__(self, entrypoint):
        self.name = '_entrypoint_waiter_{}'.format(entrypoint)
        self.entrypoint = entrypoint
        self.done = Semaphore(value=0)

    def worker_teardown(self, worker_ctx):
        provider = worker_ctx.provider
        if provider.name == self.entrypoint:
            self.done.release()

    def wait(self):
        self.done.acquire()
开发者ID:ahmb,项目名称:nameko,代码行数:22,代码来源:services.py

示例3: EntrypointWaiter

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]
class EntrypointWaiter(DependencyProvider):
    """Helper for `entrypoint_waiter`

    DependencyProvider to be manually (and temporarily) added to an existing
    container. Takes an entrypoint name, and exposes a `wait` method, which
    will return once the entrypoint has fired.
    """

    class Timeout(Exception):
        pass

    def __init__(self, entrypoint):
        self.attr_name = '_entrypoint_waiter_{}'.format(entrypoint)
        self.entrypoint = entrypoint
        self.done = Semaphore(value=0)

    def worker_teardown(self, worker_ctx):
        entrypoint = worker_ctx.entrypoint
        if entrypoint.method_name == self.entrypoint:
            self.done.release()

    def wait(self):
        self.done.acquire()
开发者ID:Costeijn,项目名称:nameko,代码行数:25,代码来源:services.py

示例4: Coroutine

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]
class Coroutine(object):
    """
    This class simulates a coroutine, which is ironic, as greenlet actually
    *is* a coroutine. But trying to use greenlet here gives nasty results
    since eventlet thoroughly monkey-patches things, making it difficult
    to run greenlet on its own.

    Essentially think of this as a wrapper for eventlet's threads which has a
    run and sleep function similar to old school coroutines, meaning it won't
    start until told and when asked to sleep it won't wake back up without
    permission.
    """

    ALL = []

    def __init__(self, func, *args, **kwargs):
        self.my_sem = Semaphore(0)   # This is held by the thread as it runs.
        self.caller_sem = None
        self.dead = False
        started = Event()
        self.id = 5
        self.ALL.append(self)

        def go():
            self.id = eventlet.corolocal.get_ident()
            started.send(True)
            self.my_sem.acquire(blocking=True, timeout=None)
            try:
                func(*args, **kwargs)
            # except Exception as e:
            #     print("Exception in coroutine! %s" % e)
            finally:
                self.dead = True
                self.caller_sem.release()  # Relinquish control back to caller.
                for i in range(len(self.ALL)):
                    if self.ALL[i].id == self.id:
                        del self.ALL[i]
                        break

        true_spawn(go)
        started.wait()

    @classmethod
    def get_current(cls):
        """Finds the coroutine associated with the thread which calls it."""
        return cls.get_by_id(eventlet.corolocal.get_ident())

    @classmethod
    def get_by_id(cls, id):
        for cr in cls.ALL:
            if cr.id == id:
                return cr
        raise RuntimeError("Coroutine with id %s not found!" % id)

    def sleep(self):
        """Puts the coroutine to sleep until run is called again.

        This should only be called by the thread which owns this object.
        """
        # Only call this from its own thread.
        assert eventlet.corolocal.get_ident() == self.id
        self.caller_sem.release()  # Relinquish control back to caller.
        self.my_sem.acquire(blocking=True, timeout=None)

    def run(self):
        """Starts up the thread. Should be called from a different thread."""
        # Don't call this from the thread which it represents.
        assert eventlet.corolocal.get_ident() != self.id
        self.caller_sem = Semaphore(0)
        self.my_sem.release()
        self.caller_sem.acquire()  # Wait for it to finish.
开发者ID:Hopebaytech,项目名称:trove,代码行数:73,代码来源:event_simulator.py

示例5: Pool

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]
class Pool(object):
    def __init__(self, min_size=0, max_size=4, track_events=False):
        if min_size > max_size:
            raise ValueError('min_size cannot be bigger than max_size')
        self.max_size = max_size
        self.sem = Semaphore(max_size)
        self.procs = proc.RunningProcSet()
        if track_events:
            self.results = coros.queue()
        else:
            self.results = None

    def resize(self, new_max_size):
        """ Change the :attr:`max_size` of the pool.

        If the pool gets resized when there are more than *new_max_size*
        coroutines checked out, when they are returned to the pool they will be
        discarded.  The return value of :meth:`free` will be negative in this
        situation.
        """
        max_size_delta = new_max_size - self.max_size
        self.sem.counter += max_size_delta
        self.max_size = new_max_size

    @property
    def current_size(self):
        """ The number of coroutines that are currently executing jobs. """
        return len(self.procs)

    def free(self):
        """ Returns the number of coroutines that are available for doing
        work."""
        return self.sem.counter

    def execute(self, func, *args, **kwargs):
        """Execute func in one of the coroutines maintained
        by the pool, when one is free.

        Immediately returns a :class:`~eventlet.proc.Proc` object which can be
        queried for the func's result.

        >>> pool = Pool()
        >>> task = pool.execute(lambda a: ('foo', a), 1)
        >>> task.wait()
        ('foo', 1)
        """
        # if reentering an empty pool, don't try to wait on a coroutine freeing
        # itself -- instead, just execute in the current coroutine
        if self.sem.locked() and api.getcurrent() in self.procs:
            p = proc.spawn(func, *args, **kwargs)
            try:
                p.wait()
            except:
                pass
        else:
            self.sem.acquire()
            p = self.procs.spawn(func, *args, **kwargs)
            # assuming the above line cannot raise
            p.link(lambda p: self.sem.release())
        if self.results is not None:
            p.link(self.results)
        return p

    execute_async = execute

    def _execute(self, evt, func, args, kw):
        p = self.execute(func, *args, **kw)
        p.link(evt)
        return p

    def waitall(self):
        """ Calling this function blocks until every coroutine
        completes its work (i.e. there are 0 running coroutines)."""
        return self.procs.waitall()

    wait_all = waitall

    def wait(self):
        """Wait for the next execute in the pool to complete,
        and return the result."""
        return self.results.wait()

    def waiting(self):
        """Return the number of coroutines waiting to execute.
        """
        if self.sem.balance < 0:
            return -self.sem.balance
        else:
            return 0

    def killall(self):
        """ Kill every running coroutine as immediately as possible."""
        return self.procs.killall()

    def launch_all(self, function, iterable):
        """For each tuple (sequence) in *iterable*, launch ``function(*tuple)``
        in its own coroutine -- like ``itertools.starmap()``, but in parallel.
        Discard values returned by ``function()``. You should call
        ``wait_all()`` to wait for all coroutines, newly-launched plus any
        previously-submitted :meth:`execute` or :meth:`execute_async` calls, to
#.........这里部分代码省略.........
开发者ID:xowenx,项目名称:eventlet,代码行数:103,代码来源:pool.py

示例6: DTestQueue

# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import release [as 别名]

#.........这里部分代码省略.........
                if dt._partner is None:
                    if len(dt._revdeps) == 0:
                        willskip = True
                else:
                    if len(dt._revdeps) == 1:
                        willskip = True

            # OK, mark it skipped if we're skipping
            if willskip:
                dt._skipped(self.output)
            else:
                waiting.append(dt)

        # OK, last pass: generate list of waiting tests; have to
        # filter out SKIPPED tests
        self.waiting = set([dt for dt in self.tests if dt.state != SKIPPED])

        # Install the capture proxies...
        if not debug:
            capture.install()

        # Spawn waiting tests
        self._spawn(self.waiting)

        # Wait for all tests to finish
        if self.th_count > 0:
            self.th_event.wait()

        # OK, uninstall the capture proxies
        if not debug:
            capture.uninstall()

        # Now we go through and clean up all left-over resources
        self.res_mgr.release_all()

        # Walk through the tests and output the results
        cnt = {
            OK: 0,
            UOK: 0,
            SKIPPED: 0,
            FAIL: 0,
            XFAIL: 0,
            ERROR: 0,
            DEPFAIL: 0,
            'total': 0,
            'threads': self.th_max,
            }
        for t in self.tests:
            # Get the result object
            r = t.result

            # Update the counts
            cnt[r.state] += int(r.test)
            cnt['total'] += int(r.test)

            # Special case update for unexpected OKs and expected failures
            if r.state == UOK:
                cnt[OK] += int(r.test)
            elif r.state == XFAIL:
                cnt[FAIL] += int(r.test)

            try:
                # Emit the result messages
                self.output.result(r, debug)
            except TypeError:
                # Maybe the output object is written to the older
开发者ID:klmitch,项目名称:dtest,代码行数:70,代码来源:core.py


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