Python threading._Event() Examples

The following are code examples for showing how to use threading._Event(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: monitoring-integration   Author: Tendrl   File: test_manager.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_main(com_start, webhook, dashboard, create,
              port, load, get_id):
    manager.monitoring_integration.MonitoringIntegrationNS = \
        test_init.init
    get_id.return_value = 1
    load.return_value = mock.MagicMock()
    webhook.return_value = mock.MagicMock()
    dashboard.return_value = True
    create.return_value = True
    port.return_value = True
    manager.TendrlNS = mock.MagicMock()
    manager.sync = mock.MagicMock()
    com_start.return_value = True
    with mock.patch.object(threading._Event, "wait", wait):
        with pytest.raises(TimeOutException):
            manager.main() 
Example 2
Project: vnpy_crypto   Author: birforce   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 3
Project: Computable   Author: ktraunmueller   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 4
Project: blatann   Author: ThomasGerstenberg   File: _threading.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _or(self, other):
    if isinstance(self, _OrEvent):
        events = self.events[:]
    elif isinstance(self, threading._Event):
        events = [self]
    else:
        raise ValueError("Incompatible Event type to OR with")

    if isinstance(other, _OrEvent):
        events.extend(other.events)
    elif isinstance(other, threading._Event):
        events.append(other)
    else:
        raise ValueError("Incompatible Event type to OR with")

    return _OrEvent(*events) 
Example 5
Project: TFTHelper   Author: AliMZaini   File: test__monkey.py    MIT License 6 votes vote down vote up
def test_thread(self):
        try:
            import thread
        except ImportError:
            import _thread as thread
        import threading

        from gevent import thread as gthread
        self.assertIs(thread.start_new_thread, gthread.start_new_thread)
        self.assertIs(threading._start_new_thread, gthread.start_new_thread)

        # Event patched by default
        self.assertTrue(monkey.is_object_patched('threading', 'Event'))

        if sys.version_info[0] == 2:
            from gevent import threading as gthreading
            from gevent.event import Event as GEvent
            self.assertIs(threading._sleep, gthreading._sleep)
            self.assertTrue(monkey.is_object_patched('threading', '_Event'))
            self.assertIs(threading._Event, GEvent) 
Example 6
Project: pySINDy   Author: luckystarufo   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 7
Project: Deribit_funding_rate_indicator   Author: Dimasik007   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 8
Project: pyebpf   Author: dany74q   File: ebpf_wrapper.py    MIT License 6 votes vote down vote up
def __init__(self, event, bpf, polling_thread_event=None):
        # type: (types.StringTypes, BPF, [Event]) -> None

        """
        :param event: str - An event name
        :param bpf: BPF - A BPF object with a compiled module
        :param polling_thread_event: Event - An event to set once the kprobe is detached
        """

        assert_type(types.StringTypes, event=event)
        assert_type(BPF, bpf=bpf)

        if polling_thread_event is not None:
            assert_type(_Event, polling_thread_event=polling_thread_event)

        self.event = normalize_event(event)
        self.bpf = bpf
        self.polling_thread_event = polling_thread_event 
Example 9
Project: trex-http-proxy   Author: alwye   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 10
Project: trex-http-proxy   Author: alwye   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 11
Project: trex-http-proxy   Author: alwye   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 12
Project: trex-http-proxy   Author: alwye   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 13
Project: trex-http-proxy   Author: alwye   File: tracker.py    MIT License 6 votes vote down vote up
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)) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_multiprocessing.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
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)

#
#
# 
Example 15
Project: ironpython2   Author: IronLanguages   File: test_multiprocessing.py    Apache License 2.0 5 votes vote down vote up
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)

#
#
# 
Example 16
Project: vbcg   Author: nspi   File: test_video.py    GNU General Public License v3.0 5 votes vote down vote up
def test_get_event_camera_ready(self):
        assert_is_instance(self.videoThread.get_event_camera_ready(), threading._Event) 
Example 17
Project: vbcg   Author: nspi   File: test_video.py    GNU General Public License v3.0 5 votes vote down vote up
def test_get_event_camera_chosen(self):
        assert_is_instance(self.videoThread.get_event_camera_chosen(), threading._Event) 
Example 18
Project: oss-ftp   Author: aliyun   File: test_multiprocessing.py    MIT License 5 votes vote down vote up
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)

#
#
# 
Example 19
Project: NiujiaoDebugger   Author: MrSrc   File: _test_multiprocessing.py    GNU General Public License v3.0 4 votes vote down vote up
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)
        p.join()

#
# 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. 
Example 20
Project: pyebpf   Author: dany74q   File: ebpf_wrapper.py    MIT License 4 votes vote down vote up
def _read_buffer_pool(self, bpf, callback, data_class, detach_event):
        # type: (BPF, types.FunctionType, types.TypeType, _Event) -> None

        """
        A routine that will be called from a separate thread, that will call a given python callback, till
        detach_event is set.

        :param bpf: A BPF instance
        :param callback: A callback to call to whenever our kprobe was invoked
        :param data_class: A ctypes data class to pass to the routine as a kwarg
        :param detach_event: An event to check against, once this is set, our thread will stop polling
        """
        assert_type(BPF, bpf=bpf)
        assert_type(types.FunctionType, callback=callback)
        assert_type(types.TypeType, data_class=data_class)
        assert_type(_Event, detach_event=detach_event)

        thread_name = current_thread().getName()

        def call_callback(cpu, data, size):
            # type: (types.IntType, types.TypeType, types.IntType) -> None

            """
            On every BPF map poll, we'll call this wrapper, which will call the passed callback.

            :param cpu: CPU #
            :param data: Shread data class from our EBPF routine
            :param size: Size of data class, in bytes
            """
            # noinspection PyUnresolvedReferences,PyBroadException
            try:
                # noinspection PyUnresolvedReferences
                data = ct.cast(data, ct.POINTER(data_class)).contents
            except Exception:
                # Try cast data best effortly
                pass

            if not detach_event.is_set():
                # noinspection PyBroadException
                try:
                    callback(cpu=cpu, data=data, size=size)
                except Exception:
                    # Best-effort callback
                    self.logger.exception('Failed calling callback')

        # noinspection PyUnresolvedReferences
        bpf['events'].open_perf_buffer(call_callback)
        while not detach_event.is_set():
            # noinspection PyBroadException
            try:
                # noinspection PyUnresolvedReferences
                bpf.perf_buffer_poll()
            except Exception:
                # Best-effort polling, if we fail - we should kill the thread
                self.logger.exception('Failed polling from bpf map')
                break

        self.logger.info('Polling thread is detached name={}'.format(thread_name)) 
Example 21
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: _test_multiprocessing.py    GNU General Public License v3.0 4 votes vote down vote up
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. 
Example 22
Project: cells   Author: AlesTsurko   File: _test_multiprocessing.py    MIT License 4 votes vote down vote up
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)
        p.join()

#
# 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.