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


Python semaphore.Semaphore类代码示例

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


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

示例1: __init__

    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()
开发者ID:Hopebaytech,项目名称:trove,代码行数:26,代码来源:event_simulator.py

示例2: run

 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,代码行数:7,代码来源:event_simulator.py

示例3: __init__

 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)
开发者ID:russss,项目名称:mjpegproxy,代码行数:8,代码来源:mjpegproxy.py

示例4: test_exceptionleaks

    def test_exceptionleaks(self):
        # tests expected behaviour with all versions of greenlet
        def test_gt(sem):
            try:
                raise KeyError()
            except KeyError:
                sem.release()
                hubs.get_hub().switch()

        # semaphores for controlling execution order
        sem = Semaphore()
        sem.acquire()
        g = eventlet.spawn(test_gt, sem)
        try:
            sem.acquire()
            assert sys.exc_info()[0] is None
        finally:
            g.kill()
开发者ID:xowenx,项目名称:eventlet,代码行数:18,代码来源:hub_test.py

示例5: __init__

 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
开发者ID:xowenx,项目名称:eventlet,代码行数:10,代码来源:pool.py

示例6: EntrypointWaiter

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,代码行数:20,代码来源:services.py

示例7: __init__

    def __init__(self, maxth=None, skip=lambda dt: dt.skip,
                 output=DTestOutput()):
        """
        Initialize a DTestQueue.  The ``maxth`` argument must be
        either None or an integer specifying the maximum number of
        simultaneous threads permitted.  The ``skip`` arguments is
        function references; it should take a test and return True if
        the test should be skipped.  The ``output`` argument should be
        an instance of DTestOutput containing a notify() method, which
        takes a test and the state to which it is transitioning, and
        may use that information to emit a test result.  Note that the
        notify() method will receive state transitions to the RUNNING
        state, as well as state transitions for test fixtures; callers
        may find the DTestBase.istest() method useful for
        differentiating between regular tests and test fixtures for
        reporting purposes.
        """

        # Save our maximum thread count
        if maxth is None:
            self.sem = None
        else:
            self.sem = Semaphore(maxth)

        # Need to remember the skip routine
        self.skip = skip

        # Also remember the output
        self.output = output

        # Initialize the lists of tests
        self.tests = set()
        self.waiting = None
        self.runlist = set()

        # No initial resource manager...
        self.res_mgr = resource.ResourceManager()

        # Need locks for the waiting and runlist lists
        self.waitlock = Semaphore()
        self.runlock = Semaphore()

        # Set up some statistics...
        self.th_count = 0
        self.th_event = Event()
        self.th_simul = 0
        self.th_max = 0

        # Place to keep any exceptions we encounter within dtest
        # itself
        self.caught = []

        # We're not yet running
        self.running = False
开发者ID:klmitch,项目名称:dtest,代码行数:54,代码来源:core.py

示例8: EntrypointWaiter

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,代码行数:23,代码来源:services.py

示例9: __init__

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,代码行数:53,代码来源:mjpegproxy.py

示例10: Coroutine

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,代码行数:71,代码来源:event_simulator.py

示例11: Pool

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,代码行数:101,代码来源:pool.py

示例12: __init__

 def __init__(self, entrypoint):
     self.attr_name = '_entrypoint_waiter_{}'.format(entrypoint)
     self.entrypoint = entrypoint
     self.done = Semaphore(value=0)
开发者ID:Costeijn,项目名称:nameko,代码行数:4,代码来源:services.py

示例13: DTestQueue

class DTestQueue(object):
    """
    DTestQueue
    ==========

    The DTestQueue class maintains a queue of tests waiting to be run.
    The constructor initializes the queue to an empty state and stores
    a maximum simultaneous thread count ``maxth`` (None means
    unlimited); a ``skip`` evaluation routine (defaults to testing the
    ``skip`` attribute of the test); and an instance of DTestOutput.
    The list of all tests in the queue is maintained in the ``tests``
    attribute; tests may be added to a queue with add_test() (for a
    single test) or add_tests() (for a sequence of tests).  The tests
    in the queue may be run by invoking the run() method.
    """

    def __init__(self, maxth=None, skip=lambda dt: dt.skip,
                 output=DTestOutput()):
        """
        Initialize a DTestQueue.  The ``maxth`` argument must be
        either None or an integer specifying the maximum number of
        simultaneous threads permitted.  The ``skip`` arguments is
        function references; it should take a test and return True if
        the test should be skipped.  The ``output`` argument should be
        an instance of DTestOutput containing a notify() method, which
        takes a test and the state to which it is transitioning, and
        may use that information to emit a test result.  Note that the
        notify() method will receive state transitions to the RUNNING
        state, as well as state transitions for test fixtures; callers
        may find the DTestBase.istest() method useful for
        differentiating between regular tests and test fixtures for
        reporting purposes.
        """

        # Save our maximum thread count
        if maxth is None:
            self.sem = None
        else:
            self.sem = Semaphore(maxth)

        # Need to remember the skip routine
        self.skip = skip

        # Also remember the output
        self.output = output

        # Initialize the lists of tests
        self.tests = set()
        self.waiting = None
        self.runlist = set()

        # No initial resource manager...
        self.res_mgr = resource.ResourceManager()

        # Need locks for the waiting and runlist lists
        self.waitlock = Semaphore()
        self.runlock = Semaphore()

        # Set up some statistics...
        self.th_count = 0
        self.th_event = Event()
        self.th_simul = 0
        self.th_max = 0

        # Place to keep any exceptions we encounter within dtest
        # itself
        self.caught = []

        # We're not yet running
        self.running = False

    def add_test(self, tst):
        """
        Add a test ``tst`` to the queue.  Tests can be added multiple
        times, but the test will only be run once.
        """

        # Can't add a test if the queue is running
        if self.running:
            raise DTestException("Cannot add tests to a running queue.")

        # First we need to get the test object
        dt = test._gettest(tst)

        # Add it to the set of tests
        self.tests.add(dt)

    def add_tests(self, tests):
        """
        Add a sequence of tests ``tests`` to the queue.  Tests can be
        added multiple times, but the test will only be run once.
        """

        # Can't add a test if the queue is running
        if self.running:
            raise DTestException("Cannot add tests to a running queue.")

        # Run add_test() in a loop
        for tst in tests:
            self.add_test(tst)
#.........这里部分代码省略.........
开发者ID:klmitch,项目名称:dtest,代码行数:101,代码来源:core.py


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