當前位置: 首頁>>代碼示例>>Python>>正文


Python threading._Event方法代碼示例

本文整理匯總了Python中threading._Event方法的典型用法代碼示例。如果您正苦於以下問題:Python threading._Event方法的具體用法?Python threading._Event怎麽用?Python threading._Event使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在threading的用法示例。


在下文中一共展示了threading._Event方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def __init__(self, *towatch):
        """MessageTracker(*towatch)

        Create a message tracker to track a set of mesages.

        Parameters
        ----------
        *towatch : tuple of Event, MessageTracker, Message instances.
            This list of objects to track. This class can track the low-level
            Events used by the Message class, other MessageTrackers or 
            actual Messages.
        """
        self.events = set()
        self.peers = set()
        for obj in towatch:
            if isinstance(obj, Event):
                self.events.add(obj)
            elif isinstance(obj, MessageTracker):
                self.peers.add(obj)
            elif isinstance(obj, Frame):
                if not obj.tracker:
                    raise ValueError("Not a tracked message")
                self.peers.add(obj.tracker)
            else:
                raise TypeError("Require Events or Message Frames, not %s"%type(obj)) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:27,代碼來源:tracker.py

示例2: test_event_api_compat

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def test_event_api_compat(self, mock_eventlet):
        with mock.patch('oslo_utils.eventletutils.is_monkey_patched',
                        return_value=True):
            e_event = eventletutils.Event()
        self.assertIsInstance(e_event, eventletutils.EventletEvent)

        t_event = eventletutils.Event()
        if six.PY3:
            t_event_cls = threading.Event
        else:
            t_event_cls = threading._Event
        self.assertIsInstance(t_event, t_event_cls)

        public_methods = [m for m in dir(t_event) if not m.startswith("_") and
                          callable(getattr(t_event, m))]

        for method in public_methods:
            self.assertTrue(hasattr(e_event, method))

        # Ensure set() allows multiple invocations, same as in
        # threading implementation.
        e_event.set()
        self.assertTrue(e_event.isSet())
        e_event.set()
        self.assertTrue(e_event.isSet()) 
開發者ID:openstack,項目名稱:oslo.utils,代碼行數:27,代碼來源:test_eventletutils.py

示例3: test_event

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def test_event(self):
        event = self.Event()
        wait = TimingWrapper(event.wait)

        # Removed temporarily, due to API shear, this does not
        # work with threading._Event objects. is_set == isSet
        self.assertEqual(event.is_set(), False)

        # Removed, threading.Event.wait() will return the value of the __flag
        # instead of None. API Shear with the semaphore backed mp.Event
        self.assertEqual(wait(0.0), False)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        self.assertEqual(wait(TIMEOUT1), False)
        self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1)

        event.set()

        # See note above on the API differences
        self.assertEqual(event.is_set(), True)
        self.assertEqual(wait(), True)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        self.assertEqual(wait(TIMEOUT1), True)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        # self.assertEqual(event.is_set(), True)

        event.clear()

        #self.assertEqual(event.is_set(), False)

        p = self.Process(target=self._test_event, args=(event,))
        p.daemon = True
        p.start()
        self.assertEqual(wait(), True)

#
#
# 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:39,代碼來源:test_multiprocessing.py

示例4: wait

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def wait(self, timeout = None):
            super(_Event, self).wait(timeout = timeout)
            return self.is_set() 
開發者ID:OpenMTC,項目名稱:OpenMTC,代碼行數:5,代碼來源:__init__.py

示例5: __setitem__

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def __setitem__(self, key, value):
        """Set the cached value for the given key."""
        existing = self.get(key)
        dict.__setitem__(self, key, value)
        if isinstance(existing, threading._Event):
            # Set Event.result so other threads waiting on it have
            # immediate access without needing to poll the cache again.
            existing.result = value
            existing.set() 
開發者ID:binhex,項目名稱:moviegrabber,代碼行數:11,代碼來源:caching.py

示例6: test_ok_received

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def test_ok_received(self):
        self.assertIsInstance(self.p._ok_received, Event)
        self.assertTrue(self.p._ok_received.is_set()) 
開發者ID:jminardi,項目名稱:mecode,代碼行數:5,代碼來源:test_printer.py

示例7: test_event

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def test_event(self):
        event = self.Event()
        wait = TimingWrapper(event.wait)

        # Removed temporarily, due to API shear, this does not
        # work with threading._Event objects. is_set == isSet
        self.assertEqual(event.is_set(), False)

        # Removed, threading.Event.wait() will return the value of the __flag
        # instead of None. API Shear with the semaphore backed mp.Event
        self.assertEqual(wait(0.0), False)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        self.assertEqual(wait(TIMEOUT1), False)
        self.assertTimingAlmostEqual(wait.elapsed, TIMEOUT1)

        event.set()

        # See note above on the API differences
        self.assertEqual(event.is_set(), True)
        self.assertEqual(wait(), True)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        self.assertEqual(wait(TIMEOUT1), True)
        self.assertTimingAlmostEqual(wait.elapsed, 0.0)
        # self.assertEqual(event.is_set(), True)

        event.clear()

        #self.assertEqual(event.is_set(), False)

        p = self.Process(target=self._test_event, args=(event,))
        p.daemon = True
        p.start()
        self.assertEqual(wait(), True)

#
# Tests for Barrier - adapted from tests in test/lock_tests.py
#

# Many of the tests for threading.Barrier use a list as an atomic
# counter: a value is appended to increment the counter, and the
# length of the list gives the value.  We use the class DummyList
# for the same purpose. 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:44,代碼來源:_test_multiprocessing.py

示例8: wait

# 需要導入模塊: import threading [as 別名]
# 或者: from threading import _Event [as 別名]
def wait(self, key, timeout=5, debug=False):
        """Return the cached value for the given key, or None.
        
        If timeout is not None, and the value is already
        being calculated by another thread, wait until the given timeout has
        elapsed. If the value is available before the timeout expires, it is
        returned. If not, None is returned, and a sentinel placed in the cache
        to signal other threads to wait.
        
        If timeout is None, no waiting is performed nor sentinels used.
        """
        value = self.get(key)
        if isinstance(value, threading._Event):
            if timeout is None:
                # Ignore the other thread and recalc it ourselves.
                if debug:
                    cherrypy.log('No timeout', 'TOOLS.CACHING')
                return None
            
            # Wait until it's done or times out.
            if debug:
                cherrypy.log('Waiting up to %s seconds' % timeout, 'TOOLS.CACHING')
            value.wait(timeout)
            if value.result is not None:
                # The other thread finished its calculation. Use it.
                if debug:
                    cherrypy.log('Result!', 'TOOLS.CACHING')
                return value.result
            # Timed out. Stick an Event in the slot so other threads wait
            # on this one to finish calculating the value.
            if debug:
                cherrypy.log('Timed out', 'TOOLS.CACHING')
            e = threading.Event()
            e.result = None
            dict.__setitem__(self, key, e)
            
            return None
        elif value is None:
            # Stick an Event in the slot so other threads wait
            # on this one to finish calculating the value.
            if debug:
                cherrypy.log('Timed out', 'TOOLS.CACHING')
            e = threading.Event()
            e.result = None
            dict.__setitem__(self, key, e)
        return value 
開發者ID:binhex,項目名稱:moviegrabber,代碼行數:48,代碼來源:caching.py


注:本文中的threading._Event方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。