Python threading 模块,Event() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用threading.Event()

项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def value(self, timeout=None):
        """Get last value 'yield'ed / value of StopIteration of task.

        NB: This method should _not_ be called from a task! This method is meant
        for main thread in the user program to wait for (main) task(s) it
        creates.

        Once task stops (finishes) executing, the last value is returned.
        """
        value = None
        self._scheduler._lock.acquire()
        if self._complete is None:
            self._complete = threading.Event()
            self._scheduler._lock.release()
            if self._complete.wait(timeout=timeout) is True:
                value = self._value
        elif self._complete == 0:
            self._scheduler._lock.release()
            value = self._value
        else:
            self._scheduler._lock.release()
            if self._complete.wait(timeout=timeout) is True:
                value = self._value
        return value
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def finish(self, timeout=None):
        """Get last value 'yield'ed / value of StopIteration of task. Must be
        used in a task with 'yield' as 'value = yield other_task.finish()'

        Once task stops (finishes) executing, the last value is returned.
        """
        value = None
        if self._complete is None:
            self._complete = Event()
            if (yield self._complete.wait(timeout=timeout)) is True:
                value = self._value
        elif self._complete == 0:
            value = self._value
        elif isinstance(self._complete, Event):
            if (yield self._complete.wait(timeout=timeout)) is True:
                value = self._value
        else:
            raise RuntimeError('invalid wait on %s/%s: %s' %
                               (self._name, self._id, type(self._complete)))
        raise StopIteration(value)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def value(self, timeout=None):
        """Get last value 'yield'ed / value of StopIteration of task.

        NB: This method should _not_ be called from a task! This method is meant
        for main thread in the user program to wait for (main) task(s) it
        creates.

        Once task stops (finishes) executing, the last value is returned.
        """
        value = None
        self._scheduler._lock.acquire()
        if self._complete is None:
            self._complete = threading.Event()
            self._scheduler._lock.release()
            if self._complete.wait(timeout=timeout) is True:
                value = self._value
        elif self._complete == 0:
            self._scheduler._lock.release()
            value = self._value
        else:
            self._scheduler._lock.release()
            if self._complete.wait(timeout=timeout) is True:
                value = self._value
        return value
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def finish(self, timeout=None):
        """Get last value 'yield'ed / value of StopIteration of task. Must be
        used in a task with 'yield' as 'value = yield other_task.finish()'

        Once task stops (finishes) executing, the last value is returned.
        """
        value = None
        if self._complete is None:
            self._complete = Event()
            if (yield self._complete.wait(timeout=timeout)) is True:
                value = self._value
        elif self._complete == 0:
            value = self._value
        elif isinstance(self._complete, Event):
            if (yield self._complete.wait(timeout=timeout)) is True:
                value = self._value
        else:
            raise RuntimeError('invalid wait on %s/%s: %s' %
                               (self._name, self._id, type(self._complete)))
        raise StopIteration(value)
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def __init__(self, parent):
        self.parent = parent

        # Initialize variables for input data processing
        self.data_queue = Queue.Queue()
        self.empty_queue = False

        # variables for thread management
        self.is_running = True
        self.timeout_check_period = 0.1 # this is in seconds
        self.process_thread_released = False

        # create mutex locks for handling issues with Reset
        self.reset_lock = threading.Lock()
        self.reset_signal = threading.Event()

        # create and start the main thread
        self.process_thread = threading.Thread(target=self.Process)
        self.process_thread.start()
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_EventDevicePortConnection(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/MessageTestPy/MessageTestPy.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(app, None)
        app.start()
        time.sleep(2)
        components = app._get_registeredComponents()
        for component in components:
            print component.componentObject._get_identifier()
            if 'MessageReceiverPy_1' in component.componentObject._get_identifier():
                stuff = component.componentObject.query([])
        recval = any.from_any(stuff[0].value)
        self.assertEquals(6, len(recval))
        for val in recval:
            self.assertEquals('test_message' in val, True)
        app.releaseObject()
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_EventDevicePortConnectionFromPython(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/MessageTestPyCpp/MessageTestPyCpp.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(app, None)
        app.start() # kick off events
        time.sleep(2)
        components = app._get_registeredComponents()
        for component in components:
            print component.componentObject._get_identifier()
            if 'DCE:b1fe6cc1-2562-4878-9a69-f191f89a6ef8' in component.componentObject._get_identifier():
                stuff = component.componentObject.query([])
        recval = any.from_any(stuff[0].value)
        self.assertEquals(6, len(recval))
        for val in recval:
            self.assertEquals('test_message' in val, True)
        app.releaseObject() # kill producer/consumer
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_EventDevicePortConnectionCppOnly(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/MessageTestCpp/MessageTestCpp.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(app, None)
        app.start() # kick off events
        time.sleep(2)
        components = app._get_registeredComponents()
        for component in components:
            print component.componentObject._get_identifier()
            if 'MessageReceiverCpp_1' in component.componentObject._get_identifier():
                stuff = component.componentObject.query([])
        recval = any.from_any(stuff[0].value)
        self.assertEquals(6, len(recval))
        for val in recval:
            self.assertEquals('test_message' in val, True)
        app.releaseObject() # kill producer/consumer
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_ECM_CppComponent(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/ECM1/ECM1.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        self.app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(self.app, None)
        mlimit, mxmit, mrecv = self._process_results( self.app )
        self.assertNotEquals(mlimit, None )
        self.assertNotEquals(mxmit, None )
        self.assertNotEquals(mrecv, None )
        self.assertEquals(mlimit, mxmit )
        self.assertEquals(mlimit, mrecv )
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_ECM_PythonComponent(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/ECM2/ECM2.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        self.app = appFact.create(appFact._get_name(), [], [])
        mlimit, mxmit, mrecv = self._process_results( self.app )
        self.assertNotEquals(mlimit, None )
        self.assertNotEquals(mxmit, None )
        self.assertNotEquals(mrecv, None )
        self.assertEquals(mlimit, mxmit )
        self.assertEquals(mlimit, mrecv )
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_ECM_PythonComponent_Callbacks(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/ECM2/ECM2.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        self.app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(self.app, None)
        mlimit, mxmit, mrecv = self._process_results( self.app , enablecb=True)
        self.assertNotEquals(mlimit, None )
        self.assertNotEquals(mxmit, None )
        self.assertNotEquals(mrecv, None )
        self.assertEquals(mlimit, mxmit )
        self.assertEquals(mlimit, mrecv )
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_ECM_JavaComponent(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/ECM3/ECM3.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        self.app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(self.app, None)
        mlimit, mxmit, mrecv = self._process_results( self.app )
        self.assertNotEquals(mlimit, None )
        self.assertNotEquals(mxmit, None )
        self.assertNotEquals(mrecv, None )
        self.assertEquals(mlimit, mxmit )
        self.assertEquals(mlimit, mrecv )
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_ECM_JavaComponent_Callbacks(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/ECM3/ECM3.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        self.app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(self.app, None)
        mlimit, mxmit, mrecv = self._process_results( self.app, enablecb=True )
        self.assertNotEquals(mlimit, None )
        self.assertNotEquals(mxmit, None )
        self.assertNotEquals(mrecv, None )
        self.assertEquals(mlimit, mxmit )
        self.assertEquals(mlimit, mrecv )
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_EventDevicePortConnectionFromPython(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/MessageTestPyJava/MessageTestPyJava.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(app, None)
        app.start() # kick off events
        time.sleep(2)
        components = app._get_registeredComponents()
        for component in components:
            print component.componentObject._get_identifier()
            if 'DCE:b1fe6cc1-2562-4878-9a69-f191f89a6ef8' in component.componentObject._get_identifier():
                stuff = component.componentObject.query([])
        recval = any.from_any(stuff[0].value)
        self.assertEquals(6, len(recval))
        for val in recval:
            self.assertEquals('test_message' in val, True)
        app.releaseObject() # kill producer/consumer
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def test_EventDevicePortConnectionJavaOnly(self):
        self.localEvent = threading.Event()
        self.eventFlag = False

        self._devBooter, self._devMgr = self.launchDeviceManager("/nodes/test_BasicTestDevice_node/DeviceManager.dcd.xml", self._domMgr)
        self.assertNotEqual(self._devBooter, None)
        self._domMgr.installApplication("/waveforms/MessageTestJava/MessageTestJava.sad.xml")
        appFact = self._domMgr._get_applicationFactories()[0]
        self.assertNotEqual(appFact, None)
        app = appFact.create(appFact._get_name(), [], [])
        self.assertNotEqual(app, None)
        components = app._get_registeredComponents()
        app.start() # kick off events
        time.sleep(2)
        for component in components:
            print component.componentObject._get_identifier()
            if 'EventReceiveJava_1' in component.componentObject._get_identifier():
                stuff = component.componentObject.query([CF.DataType("received_messages", any.to_any(None))])
        recval = any.from_any(stuff[0].value)
        self.assertEquals(6, len(recval))
        for val in recval:
            self.assertEquals('test_message' in val, True)
        app.releaseObject() # kill producer/consumer
项目:aws-greengrass-mini-fulfillment    作者:awslabs    | 项目源码 | 文件源码
def _activate_command(self, cmd):
        """Use the shared `threading.Event` instance to signal a mini
        fulfillment shadow command to the running Control thread.
        """
        self.last_state = self.active_state
        self.active_state = cmd
        log.info("[arm._activate_command] last_state='{0}' state='{1}'".format(
            self.last_state, cmd))

        if self.active_state == 'run':
            log.info("[arm._activate_command] START RUN")
            self.cmd_event.set()
        elif self.active_state == 'stop':
            log.info("[arm._activate_command] STOP")
            self.cmd_event.clear()
        return
项目:mongodb-monitoring    作者:jruaux    | 项目源码 | 文件源码
def __init__(self, interval, min_interval, target, name=None):
        """"Run a target function periodically on a background thread.

        If the target's return value is false, the executor stops.

        :Parameters:
          - `interval`: Seconds between calls to `target`.
          - `min_interval`: Minimum seconds between calls if `wake` is
            called very often.
          - `target`: A function.
          - `name`: A name to give the underlying thread.
        """
        # threading.Event and its internal condition variable are expensive
        # in Python 2, see PYTHON-983. Use a boolean to know when to wake.
        # The executor's design is constrained by several Python issues, see
        # "periodic_executor.rst" in this repository.
        self._event = False
        self._interval = interval
        self._min_interval = min_interval
        self._target = target
        self._stopped = False
        self._thread = None
        self._name = name
项目:deb-python-cassandra-driver    作者:openstack    | 项目源码 | 文件源码
def test_single_connection(self):
        """
        Test a single connection with sequential requests.
        """
        conn = self.get_connection()
        query = "SELECT keyspace_name FROM system.schema_keyspaces LIMIT 1"
        event = Event()

        def cb(count, *args, **kwargs):
            count += 1
            if count >= 10:
                conn.close()
                event.set()
            else:
                conn.send_msg(
                    QueryMessage(query=query, consistency_level=ConsistencyLevel.ONE),
                    request_id=0,
                    cb=partial(cb, count))

        conn.send_msg(
            QueryMessage(query=query, consistency_level=ConsistencyLevel.ONE),
            request_id=0,
            cb=partial(cb, 0))
        event.wait()
项目:deb-python-cassandra-driver    作者:openstack    | 项目源码 | 文件源码
def test_single_connection_pipelined_requests(self):
        """
        Test a single connection with pipelined requests.
        """
        conn = self.get_connection()
        query = "SELECT keyspace_name FROM system.schema_keyspaces LIMIT 1"
        responses = [False] * 100
        event = Event()

        def cb(response_list, request_num, *args, **kwargs):
            response_list[request_num] = True
            if all(response_list):
                conn.close()
                event.set()

        for i in range(100):
            conn.send_msg(
                QueryMessage(query=query, consistency_level=ConsistencyLevel.ONE),
                request_id=i,
                cb=partial(cb, responses, i))

        event.wait()
项目:deb-python-cassandra-driver    作者:openstack    | 项目源码 | 文件源码
def __init__(self, session, message, query, timeout, metrics=None, prepared_statement=None,
                 retry_policy=RetryPolicy(), row_factory=None, load_balancer=None, start_time=None, speculative_execution_plan=None):
        self.session = session
        # TODO: normalize handling of retry policy and row factory
        self.row_factory = row_factory or session.row_factory
        self._load_balancer = load_balancer or session.cluster._default_load_balancing_policy
        self.message = message
        self.query = query
        self.timeout = timeout
        self._retry_policy = retry_policy
        self._metrics = metrics
        self.prepared_statement = prepared_statement
        self._callback_lock = Lock()
        self._start_time = start_time or time.time()
        self._make_query_plan()
        self._event = Event()
        self._errors = {}
        self._callbacks = []
        self._errbacks = []
        self._spec_execution_plan = speculative_execution_plan or self._spec_execution_plan
        self.attempted_hosts = []
        self._start_timer()
项目:active_stream    作者:flinder    | 项目源码 | 文件源码
def __init__(self, data, train_threshold=1):
        super(Annotator, self).__init__(name='Annotator')
        self.database = data['database']
        self.train = data['events']['train_model']
        self.stoprequest = threading.Event()
        self.n_positive = False
        self.n_negative = False
        self.train_threshold = train_threshold
        self.annotation_response = data['queues']['annotation_response']
        self.socket = data['socket']
        self.annotated_text = {}
        self.message_queue = data['queues']['messages']
        self.n_trainer_triggered = 0
        self.clf_performance = {
                'true_positive': 0,
                'true_negative': 0,
                'false_positive': 0,
                'false_negative': 0
                }
        self.first = True
项目:active_stream    作者:flinder    | 项目源码 | 文件源码
def __init__(self, data, streamer, classifier, annotator):
        super(Monitor, self).__init__(name='Monitor')
        self.database = data['database']
        self.stoprequest = threading.Event()
        self.socket = data['socket']
        self.mif_queue = data['queues']['most_important_features']
        self.limit_queue = data['queues']['limit']
        self.mif = None
        self.streamer = streamer
        self.last_count = 0
        self.clf = classifier
        self.annotator = annotator
        self.counts = []
        self.missed = 0
        self.message_queue = data['queues']['messages']
        self.report_interval = 0.3
项目:win32wifi    作者:kedos    | 项目源码 | 文件源码
def testWlanRegisterNotification(self):
        handle = WlanOpenHandle()
        wlan_ifaces = WlanEnumInterfaces(handle)
        data_type = wlan_ifaces.contents.InterfaceInfo._type_
        num = wlan_ifaces.contents.NumberOfItems
        ifaces_pointer = addressof(wlan_ifaces.contents.InterfaceInfo)
        wlan_iface_info_list = (data_type * num).from_address(ifaces_pointer)
        msg = "We expect at least one wireless interface."
        self.assertGreaterEqual(len(wlan_iface_info_list), 1, msg)

        import threading
        ev = threading.Event()

        def callback(wnd, p):
            ev.set()

        cb = WlanRegisterNotification(handle, callback)
        ev.wait(5)

        if not ev.is_set():
            self.fail("Didn't receive any notification.")
项目:sndlatr    作者:Schibum    | 项目源码 | 文件源码
def __init__(self, parent, name=None, callback=None, cb_arg=None, cb_self=False):
        self.parent = parent
        self.name = name
        self.callback = callback               # Function called to process result
        if not cb_self:
            self.callback_arg = cb_arg         # Optional arg passed to "callback"
        else:
            self.callback_arg = (self, cb_arg) # Self reference required in callback arg

        self.tag = '%s%s' % (parent.tagpre, parent.tagnum)
        parent.tagnum += 1

        self.ready = threading.Event()
        self.response = None
        self.aborted = None
        self.data = None
项目:seqlog    作者:tintoy    | 项目源码 | 文件源码
def test_batchsize_2_pre_fill(self):
        record_queue = Queue()
        record_queue.put("Item1")
        record_queue.put("Item2")

        batch_received = Event()

        def handler(record_batch):
            assert len(record_batch) == 2, \
                "Incorrect batch size (expected 2, but found {}.".format(len(record_batch))

            batch_received.set()

        consumer = QueueConsumer("Test Consumer", record_queue, handler, batch_size=2)
        consumer.start()

        batch_received.wait(timeout=2000)

        consumer.stop()
项目:seqlog    作者:tintoy    | 项目源码 | 文件源码
def test_batchsize_2_post_fill(self):
        record_queue = Queue()

        batch_received = Event()

        def handler(record_batch):
            assert len(record_batch) == 2, \
                "Incorrect batch size (expected 2, but found {}.".format(len(record_batch))

            batch_received.set()

        consumer = QueueConsumer("Test Consumer", record_queue, handler, batch_size=2)
        consumer.start()

        record_queue.put("Item1")
        record_queue.put("Item2")

        batch_received.wait(timeout=2000)

        consumer.stop()

    #
    # With flush timeout
    #
项目:seqlog    作者:tintoy    | 项目源码 | 文件源码
def test_batchsize_3_post_fill_flush_timeout(self):
        record_queue = Queue()

        batch_received = Event()

        def handler(record_batch):
            assert len(record_batch) == 2, \
                "Incorrect batch size (expected 2, but found {}.".format(len(record_batch))

            batch_received.set()

        consumer = QueueConsumer("Test Consumer", record_queue, handler, batch_size=3, auto_flush_timeout=0.2)
        consumer.start()

        record_queue.put("Item1")
        record_queue.put("Item2")
        sleep(300 / 1000)
        record_queue.put("Item3")

        batch_received.wait(timeout=2000)

        consumer.stop()
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def test_add_callback_while_closing(self):
        # Issue #635: add_callback() should raise a clean exception
        # if called while another thread is closing the IOLoop.
        closing = threading.Event()

        def target():
            other_ioloop.add_callback(other_ioloop.stop)
            other_ioloop.start()
            closing.set()
            other_ioloop.close(all_fds=True)
        other_ioloop = IOLoop()
        thread = threading.Thread(target=target)
        thread.start()
        closing.wait()
        for i in range(1000):
            try:
                other_ioloop.add_callback(lambda: None)
            except RuntimeError as e:
                self.assertEqual("IOLoop is closing", str(e))
                break
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def test_add_callback_while_closing(self):
        # Issue #635: add_callback() should raise a clean exception
        # if called while another thread is closing the IOLoop.
        closing = threading.Event()

        def target():
            other_ioloop.add_callback(other_ioloop.stop)
            other_ioloop.start()
            closing.set()
            other_ioloop.close(all_fds=True)
        other_ioloop = IOLoop()
        thread = threading.Thread(target=target)
        thread.start()
        closing.wait()
        for i in range(1000):
            try:
                other_ioloop.add_callback(lambda: None)
            except RuntimeError as e:
                self.assertEqual("IOLoop is closing", str(e))
                break
项目:noc-orchestrator    作者:DirceuSilvaLabs    | 项目源码 | 文件源码
def test_add_callback_while_closing(self):
        # Issue #635: add_callback() should raise a clean exception
        # if called while another thread is closing the IOLoop.
        closing = threading.Event()

        def target():
            other_ioloop.add_callback(other_ioloop.stop)
            other_ioloop.start()
            closing.set()
            other_ioloop.close(all_fds=True)
        other_ioloop = IOLoop()
        thread = threading.Thread(target=target)
        thread.start()
        closing.wait()
        for i in range(1000):
            try:
                other_ioloop.add_callback(lambda: None)
            except RuntimeError as e:
                self.assertEqual("IOLoop is closing", str(e))
                break
项目:sauna    作者:NicolasLM    | 项目源码 | 文件源码
def __init__(self, config=None):
        if config is None:
            config = {}
        self.config = config
        self.must_stop = threading.Event()
        self._consumers_queues = []
        if self.config.get("concurrency", 1) > 1:
            self._thread_pool = ThreadPoolExecutor(
                max_workers=self.config.get("concurrency")
            )
        else:
            self._thread_pool = None
        self.import_submodules(__name__ + '.plugins.ext')
        self.import_submodules(__name__ + '.consumers.ext')
        for extra_plugin_path in self.config.get('extra_plugins', []):
            self.import_directory_modules(extra_plugin_path)
        self._current_checks = []
        self._current_checks_lock = threading.Lock()
项目:mobot    作者:JokerQyou    | 项目源码 | 文件源码
def run_async(self, func, *args, **kwargs):
        """Queue a function (with given args/kwargs) to be run asynchronously.

        Args:
            func (function): The function to run in the thread.
            args (Optional[tuple]): Arguments to `func`.
            kwargs (Optional[dict]): Keyword arguments to `func`.

        Returns:
            Promise

        """
        # TODO: handle exception in async threads
        #       set a threading.Event to notify caller thread
        promise = Promise(func, args, kwargs)
        self.__async_queue.put(promise)
        return promise
项目:zanph    作者:zanph    | 项目源码 | 文件源码
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))
项目:routersploit    作者:reverse-shell    | 项目源码 | 文件源码
def run_threads(self, threads, target, *args, **kwargs):
        workers = []
        threads_running = threading.Event()
        threads_running.set()
        for worker_id in range(int(threads)):
            worker = threading.Thread(
                target=target,
                args=chain((threads_running,), args),
                kwargs=kwargs,
                name='worker-{}'.format(worker_id),
            )
            workers.append(worker)
            worker.start()

        start = time.time()
        try:
            while worker.isAlive():
                worker.join(1)
        except KeyboardInterrupt:
            threads_running.clear()

        for worker in workers:
            worker.join()
        print_status('Elapsed time: ', time.time() - start, 'seconds')
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def testWakerOverflow(self):
        self.failure = None
        waiter = threading.Event()
        def threadedFunction():
            # Hopefully a hundred thousand queued calls is enough to
            # trigger the error condition
            for i in xrange(100000):
                try:
                    reactor.callFromThread(lambda: None)
                except:
                    self.failure = failure.Failure()
                    break
            waiter.set()
        reactor.callInThread(threadedFunction)
        waiter.wait(120)
        if not waiter.isSet():
            self.fail("Timed out waiting for event")
        if self.failure is not None:
            return defer.fail(self.failure)
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def pamAuthenticateThread(service, user, conv):
    def _conv(items):
        try:
            d = conv(items)
        except:
            import traceback
            traceback.print_exc()
            return
        ev = threading.Event()
        def cb(r):
            ev.r = (1, r)
            ev.set()
        def eb(e):
            ev.r = (0, e)
            ev.set()
        reactor.callFromThread(d.addCallbacks, cb, eb)
        ev.wait()
        done = ev.r
        if done[0]:
            return done[1]
        else:
            raise done[1].type, done[1].value

    return callIntoPAM(service, user, _conv)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def _locate(name, location, timeout):
        """Internal use only.
        """
        if not location or location in Task._pycos._locations:
            if name[0] == '~':
                SysTask._pycos._lock.acquire()
                rtask = SysTask._pycos._rtasks.get(name, None)
                SysTask._pycos._lock.release()
            else:
                rtask = Task._pycos._rtasks.get(name, None)
            if rtask or location in Task._pycos._locations:
                raise StopIteration(rtask)
        req = _NetRequest('locate_task', kwargs={'name': name}, dst=location, timeout=timeout)
        req_id = id(req)
        req.event = Event()
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs[req_id] = req
        SysTask._pycos._lock.release()
        _Peer.send_req_to(req, location)
        if (yield req.event.wait(timeout)) is False:
            req.reply = None
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs.pop(req_id, None)
        SysTask._pycos._lock.release()
        rtask = req.reply
        raise StopIteration(rtask)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def __init__(self, name, transform=None):
        """'name' must be unique across all channels.

        'transform' is a function that can either filter or transform a
        message. If the function returns 'None', the message is filtered
        (ignored). The function is called with first parameter set to channel
        name and second parameter set to the message.
        """

        if not Channel._pycos:
            Channel._pycos = Pycos.instance()
        self._scheduler = Pycos.scheduler()
        if not self._scheduler:
            self._scheduler = Channel._pycos
        self._location = None
        if transform is not None:
            try:
                argspec = inspect.getargspec(transform)
                assert len(argspec.args) == 2
            except:
                logger.warning('invalid "transform" function ignored')
                transform = None
        self._transform = transform
        self._name = name
        if not name[0].isalnum():
            while not name[0].isalnum():
                name = name[1:]
            logger.warning('Channel name "%s" should begin with alpha-numeric character;'
                           'it is changed to "%s"', self._name, name)
            self._name = name
        self._subscribers = set()
        self._subscribe_event = Event()
        self._scheduler._lock.acquire()
        if self._name in self._scheduler._channels:
            logger.warning('duplicate channel "%s"!', self._name)
        else:
            self._scheduler._channels[self._name] = self
        self._scheduler._lock.release()
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def locate(name, location=None, timeout=None):
        """Must be used with 'yield' as
        'rchannel = yield Channel.locate("name")'.

        Returns Channel instance to registered channel at remote peers so it can
        be used to send/deliver messages..
        """
        if not Channel._pycos:
            Channel._pycos = Pycos.instance()
        if not location or location in Channel._pycos._locations:
            rchannel = Channel._pycos._channels.get(name, None)
            if rchannel or location in Channel._pycos._locations:
                raise StopIteration(rchannel)
        req = _NetRequest('locate_channel', kwargs={'name': name}, dst=location, timeout=timeout)
        req.event = Event()
        req_id = id(req)
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs[req_id] = req
        SysTask._pycos._lock.release()
        _Peer.send_req_to(req, location)
        if (yield req.event.wait(timeout)) is False:
            req.reply = None
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs.pop(req_id, None)
        SysTask._pycos._lock.release()
        rchannel = req.reply
        raise StopIteration(rchannel)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def _swap_generator(self, task):
        """Internal use only.
        """
        self._lock.acquire()
        tid = task._id
        task = self._tasks.get(tid, None)
        if task is None:
            logger.warning('invalid task %s to swap', tid)
            self._lock.release()
            return -1
        if task._callers or not task._hot_swappable:
            logger.debug('postponing hot swapping of %s', str(task))
            self._lock.release()
            return 0
        else:
            task._timeout = None
            # TODO: check that another HotSwapException is not pending?
            if task._state is None:
                # assert task._id not in self._scheduled
                # assert task._id not in self._suspended
                task._generator = task._swap_generator
                task._value = None
                if task._complete == 0:
                    task._complete = None
                elif isinstance(task._complete, Event):
                    task._complete.clear()
                self._scheduled.add(tid)
                task._state = Pycos._Scheduled
                task._hot_swappable = False
            else:
                task._exceptions.append((HotSwapException, HotSwapException(task._swap_generator)))
                # assert task._state != Pycos._AwaitIO_
                if task._state in (Pycos._Suspended, Pycos._AwaitMsg_):
                    self._suspended.discard(tid)
                    self._scheduled.add(tid)
                    task._state = Pycos._Scheduled
            if self._polling and len(self._scheduled) == 1:
                self._notifier.interrupt()
            task._swap_generator = None
        self._lock.release()
        return 0
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def _locate(name, location, timeout):
        """Internal use only.
        """
        if not location or location in Task._pycos._locations:
            if name[0] == '~':
                SysTask._pycos._lock.acquire()
                rtask = SysTask._pycos._rtasks.get(name, None)
                SysTask._pycos._lock.release()
            else:
                rtask = Task._pycos._rtasks.get(name, None)
            if rtask or location in Task._pycos._locations:
                raise StopIteration(rtask)
        req = _NetRequest('locate_task', kwargs={'name': name}, dst=location, timeout=timeout)
        req_id = id(req)
        req.event = Event()
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs[req_id] = req
        SysTask._pycos._lock.release()
        _Peer.send_req_to(req, location)
        if (yield req.event.wait(timeout)) is False:
            req.reply = None
        SysTask._pycos._lock.acquire()
        SysTask._pycos._pending_reqs.pop(req_id, None)
        SysTask._pycos._lock.release()
        rtask = req.reply
        raise StopIteration(rtask)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def __init__(self, name, transform=None):
        """'name' must be unique across all channels.

        'transform' is a function that can either filter or transform a
        message. If the function returns 'None', the message is filtered
        (ignored). The function is called with first parameter set to channel
        name and second parameter set to the message.
        """

        if not Channel._pycos:
            Channel._pycos = Pycos.instance()
        self._scheduler = Pycos.scheduler()
        if not self._scheduler:
            self._scheduler = Channel._pycos
        self._location = None
        if transform is not None:
            try:
                argspec = inspect.getargspec(transform)
                assert len(argspec.args) == 2
            except:
                logger.warning('invalid "transform" function ignored')
                transform = None
        self._transform = transform
        self._name = name
        if not name[0].isalnum():
            while not name[0].isalnum():
                name = name[1:]
            logger.warning('Channel name "%s" should begin with alpha-numeric character;'
                           'it is changed to "%s"', self._name, name)
            self._name = name
        self._subscribers = set()
        self._subscribe_event = Event()
        self._scheduler._lock.acquire()
        if self._name in self._scheduler._channels:
            logger.warning('duplicate channel "%s"!', self._name)
        else:
            self._scheduler._channels[self._name] = self
        self._scheduler._lock.release()
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def __init__(self):
        if not Pycos._instance:
            Pycos._instance = self
        self._notifier = _AsyncNotifier()
        self._locations = set()
        self._location = None
        self._name = ''
        self.__cur_task = None
        self._tasks = {}
        self._scheduled = set()
        self._suspended = set()
        self._timeouts = []
        self._quit = False
        self._daemons = 0
        self._channels = {}
        self._rtasks = {}
        self._rchannels = {}
        self._atexit = []
        self._polling = False
        self._lock = threading.RLock()
        self._complete = threading.Event()
        self._complete.set()
        self._scheduler = threading.Thread(target=self._schedule)
        Pycos._schedulers[id(self._scheduler)] = self
        self._scheduler.daemon = True
        self._scheduler.start()
        if Pycos._instance == self:
            atexit.register(self.finish)
            logger.info('version %s with %s I/O notifier', __version__, self._notifier._poller_name)
项目:pycos    作者:pgiri    | 项目源码 | 文件源码
def _swap_generator(self, task):
        """Internal use only.
        """
        self._lock.acquire()
        tid = task._id
        task = self._tasks.get(tid, None)
        if task is None:
            logger.warning('invalid task %s to swap', tid)
            self._lock.release()
            return -1
        if task._callers or not task._hot_swappable:
            logger.debug('postponing hot swapping of %s', str(task))
            self._lock.release()
            return 0
        else:
            task._timeout = None
            # TODO: check that another HotSwapException is not pending?
            if task._state is None:
                # assert task._id not in self._scheduled
                # assert task._id not in self._suspended
                task._generator = task._swap_generator
                task._value = None
                if task._complete == 0:
                    task._complete = None
                elif isinstance(task._complete, Event):
                    task._complete.clear()
                self._scheduled.add(tid)
                task._state = Pycos._Scheduled
                task._hot_swappable = False
            else:
                task._exceptions.append((HotSwapException, HotSwapException(task._swap_generator)))
                # assert task._state != Pycos._AwaitIO_
                if task._state in (Pycos._Suspended, Pycos._AwaitMsg_):
                    self._suspended.discard(tid)
                    self._scheduled.add(tid)
                    task._state = Pycos._Scheduled
            if self._polling and len(self._scheduled) == 1:
                self._notifier.interrupt()
            task._swap_generator = None
        self._lock.release()
        return 0
项目:Stitch    作者:nathanlopez    | 项目源码 | 文件源码
def __init__(self):
        threading.Thread.__init__(self)
        self.finished = threading.Event()

        # Give these some initial values
        self.mouse_position_x = 0
        self.mouse_position_y = 0
        self.ison = {"shift":False, "caps":False}

        # Compile our regex statements.
        self.isshift = re.compile('^Shift')
        self.iscaps = re.compile('^Caps_Lock')
        self.shiftablechar = re.compile('^[a-z0-9]$|^minus$|^equal$|^bracketleft$|^bracketright$|^semicolon$|^backslash$|^apostrophe$|^comma$|^period$|^slash$|^grave$')
        self.logrelease = re.compile('.*')
        self.isspace = re.compile('^space$')

        # Assign default function actions (do nothing).
        self.KeyDown = lambda x: True
        self.KeyUp = lambda x: True
        self.MouseAllButtonsDown = lambda x: True
        self.MouseAllButtonsUp = lambda x: True

        self.contextEventMask = [X.KeyPress,X.MotionNotify]

        # Hook to our display.
        self.local_dpy = display.Display()
        self.record_dpy = display.Display()
项目:logscan    作者:magedu    | 项目源码 | 文件源码
def __init__(self):
        self.message = None
        self.__event = threading.Event()
        self.__cond = threading.Condition()
        self.__mail_queue = Queue(100)
项目:logscan    作者:magedu    | 项目源码 | 文件源码
def __init__(self, name, expr, path, interval, threshold, users, counter, notification):
        self.name = name
        self.expr = expr
        self.path = path
        self.interval = interval
        self.threshold = threshold
        self.users = users
        self.counter = counter
        self.__event = threading.Event()
        self.notification = notification
        self.matcher = Matcher(name, expr)
项目:logscan    作者:magedu    | 项目源码 | 文件源码
def __init__(self, queue, counter):
        self.queue = queue
        self.counter = counter
        self.checkers = {}
        self.queues = {}
        self.events = {}
        self.line = None
        self.__event = threading.Event()
        self.__cond = threading.Condition()
项目:logscan    作者:magedu    | 项目源码 | 文件源码
def add_checker(self, checker):
        self.checkers[checker.name] = checker
        checker.start()
        event = threading.Event()
        self.events[checker.name] = event
        threading.Thread(target=self.match, args=(checker, )).start()
项目:sipxecs-voicemail-transcription    作者:andrewsauder    | 项目源码 | 文件源码
def __init__(self , sockstr , protocol , opts=0 , listenq=50 , 
            sockChmod=0o666):
        self.sock = None
        self.opts = opts
        self.protocol = protocol
        self.listenq = int(listenq)
        self.sockChmod = sockChmod
        self.sockStr = sockstr
        self.poll = select.poll()
        self.emask = select.POLLIN | select.POLLPRI
        self.regLock = threading.Lock()
        self.sockMap = {}
        self.protoMap = {}
        self._close = threading.Event()
    # }}}

    # runAccepts() {{{