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: pyblish-win   Author: pyblish   File: test_socket.py    GNU Lesser General Public License v3.0 7 votes vote down vote up
def _setUp(self):
        self.server_ready = threading.Event()
        self.client_ready = threading.Event()
        self.done = threading.Event()
        self.queue = Queue.Queue(1)

        # Do some munging to start the client test.
        methodname = self.id()
        i = methodname.rfind('.')
        methodname = methodname[i+1:]
        test_method = getattr(self, '_' + methodname)
        self.client_thread = thread.start_new_thread(
            self.clientRun, (test_method,))

        self.__setUp()
        if not self.server_ready.is_set():
            self.server_ready.set()
        self.client_ready.wait() 
Example 2
Project: pyblish-win   Author: pyblish   File: test_io.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_threads_write(self):
        # Issue6750: concurrent writes could duplicate data
        event = threading.Event()
        with self.open(support.TESTFN, "w", buffering=1) as f:
            def run(n):
                text = "Thread%03d\n" % n
                event.wait()
                f.write(text)
            threads = [threading.Thread(target=lambda n=x: run(n))
                       for x in range(20)]
            for t in threads:
                t.start()
            time.sleep(0.02)
            event.set()
            for t in threads:
                t.join()
        with self.open(support.TESTFN) as f:
            content = f.read()
            for n in range(20):
                self.assertEqual(content.count("Thread%03d\n" % n), 1) 
Example 3
Project: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_dummy_thread_after_fork(self):
        # Issue #14308: a dummy thread in the active list doesn't mess up
        # the after-fork mechanism.
        code = """if 1:
            import thread, threading, os, time

            def background_thread(evt):
                # Creates and registers the _DummyThread instance
                threading.current_thread()
                evt.set()
                time.sleep(10)

            evt = threading.Event()
            thread.start_new_thread(background_thread, (evt,))
            evt.wait()
            assert threading.active_count() == 2, threading.active_count()
            if os.fork() == 0:
                assert threading.active_count() == 1, threading.active_count()
                os._exit(0)
            else:
                os.wait()
        """
        _, out, err = assert_python_ok("-c", code)
        self.assertEqual(out, '')
        self.assertEqual(err, '') 
Example 4
Project: pyblish-win   Author: pyblish   File: test_smtplib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def setUp(self):
        # temporarily replace sys.stdout to capture DebuggingServer output
        self.old_stdout = sys.stdout
        self.output = StringIO.StringIO()
        sys.stdout = self.output

        self._threads = test_support.threading_setup()
        self.serv_evt = threading.Event()
        self.client_evt = threading.Event()
        # Pick a random unused port by passing 0 for the port number
        self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
        # Keep a note of what port was assigned
        self.port = self.serv.socket.getsockname()[1]
        serv_args = (self.serv, self.serv_evt, self.client_evt)
        self.thread = threading.Thread(target=debugging_server, args=serv_args)
        self.thread.start()

        # wait until server thread has assigned a port number
        self.serv_evt.wait()
        self.serv_evt.clear() 
Example 5
Project: pyblish-win   Author: pyblish   File: test_smtplib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def testSend(self):
        # connect and send mail
        m = 'A test message'
        smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
        smtp.sendmail('John', 'Sally', m)
        # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
        # in asyncore.  This sleep might help, but should really be fixed
        # properly by using an Event variable.
        time.sleep(0.01)
        smtp.quit()

        self.client_evt.set()
        self.serv_evt.wait()
        self.output.flush()
        mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
        self.assertEqual(self.output.getvalue(), mexpect) 
Example 6
Project: pyblish-win   Author: pyblish   File: test_capi.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_pendingcalls_threaded(self):
        #do every callback on a separate thread
        n = 32 #total callbacks
        threads = []
        class foo(object):pass
        context = foo()
        context.l = []
        context.n = 2 #submits per thread
        context.nThreads = n // context.n
        context.nFinished = 0
        context.lock = threading.Lock()
        context.event = threading.Event()

        for i in range(context.nThreads):
            t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
            t.start()
            threads.append(t)

        self.pendingcalls_wait(context.l, n, context)

        for t in threads:
            t.join() 
Example 7
Project: pyblish-win   Author: pyblish   File: test_asynchat.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def line_terminator_check(self, term, server_chunk):
        event = threading.Event()
        s = echo_server(event)
        s.chunk_size = server_chunk
        s.start()
        event.wait()
        event.clear()
        time.sleep(0.01) # Give server time to start accepting.
        c = echo_client(term, s.port)
        c.push("hello ")
        c.push("world%s" % term)
        c.push("I'm not dead yet!%s" % term)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        s.join()

        self.assertEqual(c.contents, ["hello world", "I'm not dead yet!"])

    # the line terminator tests below check receiving variously-sized
    # chunks back from the server in order to exercise all branches of
    # async_chat.handle_read 
Example 8
Project: pyblish-win   Author: pyblish   File: test_asynchat.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_close_when_done(self):
        s, event = start_echo_server()
        s.start_resend_event = threading.Event()
        c = echo_client('\n', s.port)
        c.push("hello world\nI'm not dead yet!\n")
        c.push(SERVER_QUIT)
        c.close_when_done()
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)

        # Only allow the server to start echoing data back to the client after
        # the client has closed its connection.  This prevents a race condition
        # where the server echoes all of its data before we can check that it
        # got any down below.
        s.start_resend_event.set()
        s.join()

        self.assertEqual(c.contents, [])
        # the server might have been able to send a byte or two back, but this
        # at least checks that it received something and didn't just fail
        # (which could still result in the client not having received anything)
        self.assertTrue(len(s.buffer) > 0) 
Example 9
Project: RF-Monitor   Author: EarToEarOak   File: gps.py    GNU General Public License v2.0 6 votes vote down vote up
def __sats(self, data):
        message = int(data[1])
        messages = int(data[1])
        viewed = int(data[3])

        if message == 1:
            self._sats.clear()

        blocks = (len(data) - 4) / 4
        for i in range(0, blocks):
            sat = int(data[4 + i * 4])
            level = data[7 + i * 4]
            used = True
            if level == '':
                level = None
                used = False
            else:
                level = int(level)
            self._sats[sat] = {'Level': level,
                               'Used': used}

        if message == messages and len(self._sats) == viewed:
            event = Event(Events.GPS_SATS, sats=self._sats)
            post_event(self._eventHandler, event) 
Example 10
Project: RF-Monitor   Author: EarToEarOak   File: gps.py    GNU General Public License v2.0 6 votes vote down vote up
def __read(self):
        for resp in self.__serial_read():
            nmea = resp.split('*')
            if len(nmea) == 2:
                data = nmea[0].split(',')
                if data[0] in ['GPGGA', 'GPGSV']:
                    checksum = self.__checksum(nmea[0])
                    if checksum == nmea[1]:
                        if data[0] == 'GPGGA':
                            self.__global_fix(data)
                        elif data[0] == 'GPGSV':
                            self.__sats(data)
                    else:
                        warn = 'Invalid checksum for {} sentence'.format(data[0])
                        event = Event(Events.GPS_WARN, msg=warn)
                        post_event(self._eventHandler, event) 
Example 11
Project: gamma-collector   Author: bytting   File: gammad.py    GNU General Public License v2.0 6 votes vote down vote up
def __init__(self):

        self.client_address = None

        self.detector_state = DetectorState.Cold
        self.detector_data = None

        self.session_args = None
        self.session_loop = None
        self.session_state = SessionState.Ready

        self.spectrum_state = SpectrumState.Ready
        self.spectrum_index = 0
        self.spectrum_failures = 0

        self.database_connection = None

        self.gps_stop = threading.Event() # Event used to notify gps thread
        self.gps = gps.GpsThread(self.gps_stop)

        self.plugin = None 
Example 12
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadHost.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, pythonPath='pythonw', outBuffer=None):
        global kernelAlive
        self.queueIn = Queue.Queue()
        if not pythonPath.endswith('.exe'):
            if pythonPath.strip().endswith('pythonw'):
                pythonPath = pythonPath.strip() + '.exe'
            else:
                pythonPath = os.path.join(pythonPath.strip(),'pythonw.exe')
        assert os.path.exists(pythonPath), 'file pythonw.exe not found.'
        self.outBuffer = outBuffer
        clientPath = os.path.join(os.path.dirname(__file__), 'pyPadClient.py')
        self.StartCommand = pythonPath + ' -u ' + '"' + clientPath + '"'
        print "start kernel:", self.StartCommand
        self.kernelActive = threading.Event()
        self.kernelAlive = kernelAlive = threading.Event()
        self.startNewKernel()
        self.thread = threading.Thread(name='communicationLoop', target=self.communicationLoop, args=())
        self.thread.start() 
Example 13
Project: flasky   Author: RoseOu   File: downloads.py    MIT License 6 votes vote down vote up
def __init__(self, status, output, tick=.1, update_interval=1):
        """

        :type status: Status
        :type output: file
        """
        super(ProgressReporterThread, self).__init__()
        self.status = status
        self.output = output
        self._tick = tick
        self._update_interval = update_interval
        self._spinner_pos = 0
        self._status_line = ''
        self._prev_bytes = 0
        self._prev_time = time()
        self._should_stop = threading.Event() 
Example 14
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_thread_local.py    Apache License 2.0 6 votes vote down vote up
def test_blockscope():
    class dummy_block(object):
        def __init__(self, prefix):
            self.prefix = prefix
            self._empty_prefix = False
    blockscope_list = []
    status = [False]
    event = threading.Event()
    def f():
        with block._BlockScope(dummy_block("spawned_")):
            x= NameManager.current.get(None, "hello")
            event.wait()
            if x == "spawned_hello0":
                status[0] = True
    thread = threading.Thread(target=f)
    thread.start()
    block._BlockScope.create("main_thread", None, "hi")
    event.set()
    thread.join()
    event.clear()
    assert status[0], "Spawned thread isn't using the correct blockscope namemanager" 
Example 15
Project: myhoard   Author: aiven   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def __init__(self, *, connect_info, index_offset=0, make_temp_tables=True):
        super().__init__()
        self.basic_wait = 0.1
        self.committed_row_count = 0
        self.connect_info = connect_info
        self.estimated_bytes = 0
        self.generate_data_event = threading.Event()
        self.generate_data_event.set()
        self.index_offset = index_offset
        self.is_running = True
        self.make_temp_tables = make_temp_tables
        self.paused = False
        self.pending_row_count = 0
        self.row_count = 0
        self.row_infos = []
        self.temp_table_index = 0
        self.temp_tables = [] 
Example 16
Project: bigquerylayers   Author: smandaric   File: bidi.py    GNU General Public License v3.0 6 votes vote down vote up
def start(self):
        """Start the background thread and begin consuming the thread."""
        with self._operational_lock:
            ready = threading.Event()
            thread = threading.Thread(
                name=_BIDIRECTIONAL_CONSUMER_NAME,
                target=self._thread_main,
                args=(ready,),
            )
            thread.daemon = True
            thread.start()
            # Other parts of the code rely on `thread.is_alive` which
            # isn't sufficient to know if a thread is active, just that it may
            # soon be active. This can cause races. Further protect
            # against races by using a ready event and wait on it to be set.
            ready.wait()
            self._thread = thread
            _LOGGER.debug("Started helper thread %s", thread.name) 
Example 17
Project: smbprotocol   Author: jborean93   File: change_notify.py    MIT License 6 votes vote down vote up
def __init__(self, open):
        """
        A class that encapsulates a FileSystemWatcher over SMB. It is designed to make it easy to run the watcher in
        the background and provide an event that is fired when the server notifies that a change has occurred. It is
        up to the caller to action on that event through their own sync or asynchronous implementation.

        :param open: The Open() class of a directory to watch for change notifications.
        """
        self.open = open
        self.response_event = threading.Event()

        self._t_on_response = threading.Thread(target=self._on_response)
        self._t_on_response.daemon = True
        self._t_exc = None
        self._request = None
        self._file_actions = None
        self._result_lock = threading.Lock()  # Used to ensure the result is only processed once 
Example 18
Project: Home_Surveillance_with_Python   Author: kalfasyan   File: base_camera.py    MIT License 5 votes vote down vote up
def wait(self):
        """Invoked from each client's thread to wait for the next frame."""
        ident = get_ident()
        if ident not in self.events:
            # this is a new client
            # add an entry for it in the self.events dict
            # each entry has two elements, a threading.Event() and a timestamp
            self.events[ident] = [threading.Event(), time.time()]
        return self.events[ident][0].wait() 
Example 19
Project: clikit   Author: sdispater   File: progress_indicator.py    MIT License 5 votes vote down vote up
def auto(self, start_message, end_message):
        """
        Auto progress.
        """
        self._auto_running = threading.Event()
        self._auto_thread = threading.Thread(target=self._spin)

        self.start(start_message)
        self._auto_thread.start()

        try:
            yield self
        except (Exception, KeyboardInterrupt):
            self._io.error_line("")

            self._auto_running.set()
            self._auto_thread.join()

            raise

        self.finish(end_message, reset_indicator=True) 
Example 20
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 5 votes vote down vote up
def __init__(self, protected_path, timeout=0.0, delay=0.05):
        """Create new :class:`LockFile` object."""
        self.lockfile = protected_path + '.lock'
        self._lockfile = None
        self.timeout = timeout
        self.delay = delay
        self._lock = Event()
        atexit.register(self.release) 
Example 21
Project: pyblish-win   Author: pyblish   File: SocketServer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, server_address, RequestHandlerClass):
        """Constructor.  May be extended, do not override."""
        self.server_address = server_address
        self.RequestHandlerClass = RequestHandlerClass
        self.__is_shut_down = threading.Event()
        self.__shutdown_request = False 
Example 22
Project: pyblish-win   Author: pyblish   File: test_threading.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_ident_of_no_threading_threads(self):
        # The ident still must work for the main thread and dummy threads.
        self.assertFalse(threading.currentThread().ident is None)
        def f():
            ident.append(threading.currentThread().ident)
            done.set()
        done = threading.Event()
        ident = []
        thread.start_new_thread(f, ())
        done.wait()
        self.assertFalse(ident[0] is None)
        # Kill the "immortal" _DummyThread
        del threading._active[ident[0]]

    # run with a small(ish) thread stack size (256kB) 
Example 23
Project: pyblish-win   Author: pyblish   File: test_smtplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.old_stdout = sys.stdout
        self.output = StringIO.StringIO()
        sys.stdout = self.output

        self._threads = test_support.threading_setup()
        self.evt = threading.Event()
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.settimeout(15)
        self.port = test_support.bind_port(self.sock)
        servargs = (self.evt, "199 no hello for you!\n", self.sock)
        self.thread = threading.Thread(target=server, args=servargs)
        self.thread.start()
        self.evt.wait()
        self.evt.clear() 
Example 24
Project: pyblish-win   Author: pyblish   File: test_smtplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.old_stdout = sys.stdout
        self.output = StringIO.StringIO()
        sys.stdout = self.output

        self.evt = threading.Event()
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.settimeout(15)
        self.port = test_support.bind_port(self.sock)
        servargs = (self.evt, self.respdata, self.sock)
        threading.Thread(target=server, args=servargs).start()
        self.evt.wait()
        self.evt.clear() 
Example 25
Project: pyblish-win   Author: pyblish   File: test_smtplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self._threads = test_support.threading_setup()
        self.serv_evt = threading.Event()
        self.client_evt = threading.Event()
        # Pick a random unused port by passing 0 for the port number
        self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1))
        # Keep a note of what port was assigned
        self.port = self.serv.socket.getsockname()[1]
        serv_args = (self.serv, self.serv_evt, self.client_evt)
        self.thread = threading.Thread(target=debugging_server, args=serv_args)
        self.thread.start()

        # wait until server thread has assigned a port number
        self.serv_evt.wait()
        self.serv_evt.clear() 
Example 26
Project: pyblish-win   Author: pyblish   File: test_ssl.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __enter__(self):
            self.start(threading.Event())
            self.flag.wait()
            return self 
Example 27
Project: pyblish-win   Author: pyblish   File: test_ssl.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __enter__(self):
            self.start(threading.Event())
            self.flag.wait()
            return self 
Example 28
Project: pyblish-win   Author: pyblish   File: test_ssl.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rude_shutdown(self):
            """A brutal shutdown of an SSL server should raise an OSError
            in the client when attempting handshake.
            """
            listener_ready = threading.Event()
            listener_gone = threading.Event()

            s = socket.socket()
            port = support.bind_port(s, HOST)

            # `listener` runs in a thread.  It sits in an accept() until
            # the main thread connects.  Then it rudely closes the socket,
            # and sets Event `listener_gone` to let the main thread know
            # the socket is gone.
            def listener():
                s.listen(5)
                listener_ready.set()
                newsock, addr = s.accept()
                newsock.close()
                s.close()
                listener_gone.set()

            def connector():
                listener_ready.wait()
                with closing(socket.socket()) as c:
                    c.connect((HOST, port))
                    listener_gone.wait()
                    try:
                        ssl_sock = ssl.wrap_socket(c)
                    except socket.error:
                        pass
                    else:
                        self.fail('connecting to closed SSL socket should have failed')

            t = threading.Thread(target=listener)
            t.start()
            try:
                connector()
            finally:
                t.join() 
Example 29
Project: pyblish-win   Author: pyblish   File: test_ssl.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_server_accept(self):
            # Issue #16357: accept() on a SSLSocket created through
            # SSLContext.wrap_socket().
            context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
            context.verify_mode = ssl.CERT_REQUIRED
            context.load_verify_locations(CERTFILE)
            context.load_cert_chain(CERTFILE)
            server = socket.socket(socket.AF_INET)
            host = "127.0.0.1"
            port = support.bind_port(server)
            server = context.wrap_socket(server, server_side=True)

            evt = threading.Event()
            remote = [None]
            peer = [None]
            def serve():
                server.listen(5)
                # Block on the accept and wait on the connection to close.
                evt.set()
                remote[0], peer[0] = server.accept()
                remote[0].recv(1)

            t = threading.Thread(target=serve)
            t.start()
            # Client wait until server setup and perform a connect.
            evt.wait()
            client = context.wrap_socket(socket.socket())
            client.connect((host, port))
            client_addr = client.getsockname()
            client.close()
            t.join()
            remote[0].close()
            server.close()
            # Sanity checks.
            self.assertIsInstance(remote[0], ssl.SSLSocket)
            self.assertEqual(peer[0], client_addr) 
Example 30
Project: pyblish-win   Author: pyblish   File: test_logging.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, host='localhost',
                             port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
                     handler=LogRecordStreamHandler):
        ThreadingTCPServer.__init__(self, (host, port), handler)
        self.abort = False
        self.timeout = 0.1
        self.finished = threading.Event() 
Example 31
Project: pyblish-win   Author: pyblish   File: test_xmlrpc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        # enable traceback reporting
        SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True

        self.evt = threading.Event()
        # start server thread to handle requests
        serv_args = (self.evt, self.request_count, self.requestHandler)
        threading.Thread(target=self.threadFunc, args=serv_args).start()

        # wait for the server to be ready
        self.evt.wait(10)
        self.evt.clear() 
Example 32
Project: pyblish-win   Author: pyblish   File: test_xmlrpc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def setUp(self):
        self.evt = threading.Event()
        # start server thread to handle requests
        serv_args = (self.evt, 1)
        threading.Thread(target=http_server, args=serv_args).start()

        # wait for the server to be ready
        self.evt.wait()
        self.evt.clear() 
Example 33
Project: pyblish-win   Author: pyblish   File: test_hashlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_threaded_hashing(self):
        # Updating the same hash object from several threads at once
        # using data chunk sizes containing the same byte sequences.
        #
        # If the internal locks are working to prevent multiple
        # updates on the same object from running at once, the resulting
        # hash will be the same as doing it single threaded upfront.
        hasher = hashlib.sha1()
        num_threads = 5
        smallest_data = 'swineflu'
        data = smallest_data*200000
        expected_hash = hashlib.sha1(data*num_threads).hexdigest()

        def hash_in_chunks(chunk_size, event):
            index = 0
            while index < len(data):
                hasher.update(data[index:index+chunk_size])
                index += chunk_size
            event.set()

        events = []
        for threadnum in xrange(num_threads):
            chunk_size = len(data) // (10**threadnum)
            assert chunk_size > 0
            assert chunk_size % len(smallest_data) == 0
            event = threading.Event()
            events.append(event)
            threading.Thread(target=hash_in_chunks,
                             args=(chunk_size, event)).start()

        for event in events:
            event.wait()

        self.assertEqual(expected_hash, hasher.hexdigest()) 
Example 34
Project: pyblish-win   Author: pyblish   File: test_decimal.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_threading(self):
        #Test the "threading isolation" of a Context.

        self.synchro = threading.Event()
        self.finish1 = threading.Event()
        self.finish2 = threading.Event()

        th1 = threading.Thread(target=thfunc1, args=(self,))
        th2 = threading.Thread(target=thfunc2, args=(self,))

        th1.start()
        th2.start()

        self.finish1.wait()
        self.finish2.wait() 
Example 35
Project: BASS   Author: Cisco-Talos   File: core.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, job_id):
        self.samples = []
        self.status = self.STATUS_CREATED
        self.thread = None
        self.finished = Event()
        self.status_lock = Lock()
        self.id = job_id 
Example 36
Project: RF-Monitor   Author: EarToEarOak   File: gps.py    GNU General Public License v2.0 5 votes vote down vote up
def __timeout(self):
        self.stop()
        event = Event(Events.GPS_TIMEOUT, msg='GPS timeout')
        post_event(self._eventHandler, event) 
Example 37
Project: RF-Monitor   Author: EarToEarOak   File: gps.py    GNU General Public License v2.0 5 votes vote down vote up
def __global_fix(self, data):
        if data[6] in ['1', '2']:
            lat = self.__coord(data[2], data[3])
            lon = self.__coord(data[4], data[5])

            event = Event(Events.GPS_LOC, loc=(lat, lon))
            post_event(self._eventHandler, event) 
Example 38
Project: RF-Monitor   Author: EarToEarOak   File: gps.py    GNU General Public License v2.0 5 votes vote down vote up
def run(self):
        try:
            self.__open()
            self.__read()
        except SerialException as error:
            event = Event(Events.GPS_ERROR, msg=error.message)
            post_event(self._eventHandler, event)
        except OSError as error:
            event = Event(Events.GPS_ERROR, msg=error)
            post_event(self._eventHandler, event)
        except ValueError as error:
            event = Event(Events.GPS_ERROR, msg=error)
            post_event(self._eventHandler, event)

        self.__close() 
Example 39
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: util.py    MIT License 5 votes vote down vote up
def __init__(self, protected_path, timeout=0.0, delay=0.05):
        """Create new :class:`LockFile` object."""
        self.lockfile = protected_path + '.lock'
        self._lockfile = None
        self.timeout = timeout
        self.delay = delay
        self._lock = Event()
        atexit.register(self.release) 
Example 40
Project: AutoDL   Author: tanguofu   File: name_dict.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def start(self):
        self.event = threading.Event()
        self.thread = threading.Thread(target=self.loop)
        self.thread.start() 
Example 41
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: events.py    MIT License 5 votes vote down vote up
def myThread(myEvent):
  while not myEvent.is_set():
    print("Waiting for Event to be set")
    time.sleep(1)
  print("myEvent has been set") 
Example 42
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: events.py    MIT License 5 votes vote down vote up
def main():
  myEvent = threading.Event()
  thread1 = threading.Thread(target=myThread, args=(myEvent,))
  thread1.start()

  time.sleep(10)
  myEvent.set() 
Example 43
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadRemoteClient.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, host="127.0.0.5", port=8888):
        self.socket = socketStream(host, port)
        self.buffer = bufferOut()
        self.stdout = sys.stdout = self.socket
        self.stderr = sys.stderr = self.socket
        self.stdin = sys.stdin
        self.stdOutBuff = PseudoFileOutBuffer(self.buffer)
        self.stdErrBuff = PseudoFileOutBuffer(self.buffer, True)
        sys.stdout=self.stdOutBuff
        sys.stderr=self.stdErrBuff
        self.userLocals = {}
        self.interp = code.InteractiveInterpreter(self.userLocals)
        self.kernelBusy = threading.Event()
        os.chdir(os.path.expanduser("~")) 
Example 44
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadClient.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.buffer = bufferOut()
        self.stdout = sys.stdout
        self.stderr = sys.stderr
        self.stdin = sys.stdin
        self.stdOutBuff = PseudoFileOutBuffer(self.buffer)
        self.stdErrBuff = PseudoFileOutBuffer(self.buffer, True)
        sys.stdout=self.stdOutBuff
        sys.stderr=self.stdErrBuff
        self.userLocals = {}
        self.interp = code.InteractiveInterpreter(self.userLocals)
        self.kernelBusy = threading.Event()
        os.chdir(os.path.expanduser("~")) 
Example 45
Project: PyPadPlusPlus   Author: bitagoras   File: pyPadRemoteHost.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, host="127.0.0.5", port=8888, outBuffer=None):
        self.outBuffer = outBuffer
        clientPath = os.path.join(os.path.dirname(__file__), 'pyPadClient.py')
        #self.StartCommand = pythonPath + ' -u ' + '"' + clientPath + '"'
        self.dataQueueOut = Queue.Queue()
        self.dataQueueIn = Queue.Queue()
        self.kernelBusy = threading.Event()
        self.kernelAlive = threading.Event()
        self.startNewKernel()
        self.thread = threading.Thread(name='communicationLoop', target=self.communicationLoop, args=())
        self.thread.start() 
Example 46
Project: gluster-integration   Author: Tendrl   File: __init__.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def __init__(self):
        self._complete = threading.Event()
        super(
            GlusterIntegrationManager,
            self
        ).__init__(
            NS.state_sync_thread,
            message_handler_thread=NS.message_handler_thread
        ) 
Example 47
Project: gluster-integration   Author: Tendrl   File: __init__.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def __init__(self):
        super(GlusterIntegrationSdsSyncStateThread, self).__init__()
        self._complete = threading.Event() 
Example 48
Project: telegram-repeater   Author: googlehosts   File: tg_tools.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, client: Client, problem_set: dict, chat_id: int):
		Thread.__init__(self, daemon = True)
		self.client = client
		self.chat_id = chat_id
		self.user_dict = {}
		self.revoke_time = problem_set['revoke_time'] + 10
		self.join_group_msg = problem_set['success_msg']
		self.tricket_msg = problem_set['ticket_bot']['text']
		self.last_revoke_time = 0.0
		self.current_link = ''
		self.stop_event = Event()
		self.start() 
Example 49
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _make_shutdown_event() -> StopSignal:
        """Create an event that can signal if this task should stop. The event should provide
        `is_set()` and `set()` methods (like the `Event` from `threading` does)."""
        return threading.Event() 
Example 50
Project: pnp   Author: HazardDede   File: parallel.py    MIT License 5 votes vote down vote up
def _make_shutdown_event(self) -> StopSignal:  # pylint: disable=no-self-use
        # We might run the Engine in an isolated process.
        # So the multiprocessing.Event() is better suited.
        return multiprocessing.Event() 
Example 51
Project: pnp   Author: HazardDede   File: test_hass_state.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.stopped = Event()
        self.started = Event()
        self._thr = None 
Example 52
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: io.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, iters, rename_data=None, rename_label=None):
        super(PrefetchingIter, self).__init__()
        if not isinstance(iters, list):
            iters = [iters]
        self.n_iter = len(iters)
        assert self.n_iter > 0
        self.iters = iters
        self.rename_data = rename_data
        self.rename_label = rename_label
        self.batch_size = self.provide_data[0][1][0]
        self.data_ready = [threading.Event() for i in range(self.n_iter)]
        self.data_taken = [threading.Event() for i in range(self.n_iter)]
        for i in self.data_taken:
            i.set()
        self.started = True
        self.current_batch = [None for i in range(self.n_iter)]
        self.next_batch = [None for i in range(self.n_iter)]
        def prefetch_func(self, i):
            """Thread entry"""
            while True:
                self.data_taken[i].wait()
                if not self.started:
                    break
                try:
                    self.next_batch[i] = self.iters[i].next()
                except StopIteration:
                    self.next_batch[i] = None
                self.data_taken[i].clear()
                self.data_ready[i].set()
        self.prefetch_threads = [threading.Thread(target=prefetch_func, args=[self, i]) \
                                 for i in range(self.n_iter)]
        for thread in self.prefetch_threads:
            thread.setDaemon(True)
            thread.start() 
Example 53
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_thread_local.py    Apache License 2.0 5 votes vote down vote up
def test_context():
    ctx_list = []
    ctx_list.append(Context.default_ctx)
    def f():
        set_default_context(mx.gpu(11))
        ctx_list.append(Context.default_ctx)
    thread = threading.Thread(target=f)
    thread.start()
    thread.join()
    assert Context.devtype2str[ctx_list[0].device_typeid] == "cpu"
    assert ctx_list[0].device_id == 0
    assert Context.devtype2str[ctx_list[1].device_typeid] == "gpu"
    assert ctx_list[1].device_id == 11

    event = threading.Event()
    status = [False]
    def g():
        with mx.cpu(10):
            event.wait()
            if Context.default_ctx.device_id == 10:
                status[0] = True
    thread = threading.Thread(target=g)
    thread.start()
    Context.default_ctx = Context("cpu", 11)
    event.set()
    thread.join()
    event.clear()
    assert status[0], "Spawned thread didn't set the correct context" 
Example 54
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_thread_local.py    Apache License 2.0 5 votes vote down vote up
def test_attrscope():
    attrscope_list = []
    AttrScope.current = AttrScope(y="hi", z="hey")
    attrscope_list.append(AttrScope.current)
    def f():
        AttrScope.current = AttrScope(x="hello")
        attrscope_list.append(AttrScope.current)
    thread = threading.Thread(target=f)
    thread.start()
    thread.join()
    assert len(attrscope_list[0]._attr) == 2
    assert attrscope_list[1]._attr["x"] == "hello"

    event = threading.Event()
    status = [False]
    def g():
        with mx.AttrScope(x="hello"):
            event.wait()
            if "hello" in AttrScope.current._attr.values():
                status[0] = True
    thread = threading.Thread(target=g)
    thread.start()
    AttrScope.current = AttrScope(x="hi")
    event.set()
    thread.join()
    AttrScope.current = AttrScope()
    event.clear()
    assert status[0], "Spawned thread didn't set the correct attr key values" 
Example 55
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_thread_local.py    Apache License 2.0 5 votes vote down vote up
def test_name():
    name_list = []
    NameManager.current = NameManager()
    NameManager.current.get(None, "main_thread")
    name_list.append(NameManager.current)
    def f():
        NameManager.current = NameManager()
        NameManager.current.get(None, "spawned_thread")
        name_list.append(NameManager.current)
    thread = threading.Thread(target=f)
    thread.start()
    thread.join()
    assert "main_thread" in name_list[0]._counter, "cannot find the string `main thread` in name_list[0]._counter"
    assert "spawned_thread" in name_list[1]._counter, "cannot find the string `spawned thread` in name_list[1]._counter"

    event = threading.Event()
    status = [False]
    def g():
        with NameManager():
            if "main_thread" not in NameManager.current._counter:
                status[0] = True
    thread = threading.Thread(target=g)
    thread.start()
    NameManager.current = NameManager()
    NameManager.current.get(None, "main_thread")
    event.set()
    thread.join()
    event.clear()
    assert status[0], "Spawned thread isn't using thread local NameManager" 
Example 56
Project: Adeept_PiCar-B   Author: adeept   File: appserver.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(Servo_ctrl, self).__init__(*args, **kwargs)
        self.__flag = threading.Event()     # 用于暂停线程的标识
        self.__flag.set()       # 设置为True
        self.__running = threading.Event()      # 用于停止线程的标识
        self.__running.set()      # 将running设置为True 
Example 57
Project: alfred-urban-dictionary   Author: xilopaint   File: util.py    MIT License 5 votes vote down vote up
def __init__(self, protected_path, timeout=0.0, delay=0.05):
        """Create new :class:`LockFile` object."""
        self.lockfile = protected_path + '.lock'
        self._lockfile = None
        self.timeout = timeout
        self.delay = delay
        self._lock = Event()
        atexit.register(self.release) 
Example 58
Project: ARU-Net   Author: TobiasGruening   File: data_provider_la.py    GNU General Public License v2.0 5 votes vote down vote up
def restart_val_runner(self):
        if self.list_val != None:
            self.stopVal.set()
            self.stopVal = threading.Event()
            self.q_val, self.threads_val = self._get_list_queue(self.list_val, 1, 100, self.stopVal, self.batchsize_val, self.scale_val, self.scale_val, self.affine_val, self.elastic_val, self.rotate_val, self.rotateMod90_val) 
Example 59
Project: fishroom   Author: tuna   File: runner.py    GNU General Public License v3.0 5 votes vote down vote up
def run_threads(thread_target_args: Iterable[Tuple[AnyFunc, AnyArgs]]):
    from .telegram import Telegram
    from .config import config

    tasks = []
    DEAD = threading.Event()

    # wrapper to send report traceback info to telegram
    def die(f: AnyFunc):
        tg = Telegram(config["telegram"]["token"])
        logger = get_logger(__name__)

        def send_all(text):
            for adm in config["telegram"]["admin"]:
                try:
                    tg.send_msg(adm, text, escape=False)
                except:
                    pass

        def wrapper(*args, **kwargs):
            try:
                f(*args, **kwargs)
            except:
                logger.exception("thread failed")
                exc = traceback.format_exc()
                send_all("<code>{}</code>".format(exc))
                DEAD.set()

        return wrapper

    for target, args in thread_target_args:
        t = threading.Thread(target=die(target), args=args)
        t.setDaemon(True)
        t.start()
        tasks.append(t)

    DEAD.wait()
    logger.error("Everybody died, I don't wanna live any more! T_T")
    os._exit(1) 
Example 60
Project: deep-nn-car   Author: scope-lab-vu   File: Server.py    MIT License 5 votes vote down vote up
def dataCollectionMode(sock):
    # name of file containing timestamps and duty cycles
    newTrialCreated = False
    trialNumber = 1
    while not newTrialCreated:
        newpath = '/media/pi/USB DRIVE/Trial{0}'.format(trialNumber)
        if not os.path.exists(newpath): 
            os.makedirs(newpath)
            newTrialCreated = True
        else:
            trialNumber = trialNumber + 1
    csvfile = open("Data.csv", "w")
    pill2kill = threading.Event()
    print('Starting camera')
    thread = Thread(target=camera,args=(pill2kill,1,newpath,))
    thread.start()
    # begin recording movements
    print('Collecting duty cycles and images.')
    dataCollectionDriving(sock,csvfile)
    # after client sends STOP signal, terminates camera
    pill2kill.set()
    thread.join()
    # moves .csv files from Rpi to external USB then deletes from Rpi+
    print('Copying files to USB')
    shutil.copy2('/home/pi/Desktop/PiCar/ImageCount.csv', newpath + '/ImageCountData.csv')
    shutil.copy2('/home/pi/Desktop/PiCar/Data.csv', newpath + '/DutyCycleData.csv')

# Mode 2 
Example 61
Project: deep-nn-car   Author: scope-lab-vu   File: Server.py    MIT License 5 votes vote down vote up
def liveStreamMode(sock):
    print('Live Streaming')
    pill2kill = threading.Event()
    thread = Thread(target=camera,args=(pill2kill,3,0,))
    thread.start()
    # begin recording movements
    print('Live stream started.')   
    liveStreamDriving(sock)
    # after client sends STOP signal, terminates camera
    pill2kill.set()
    thread.join()

# MAIN 
Example 62
Project: python-lifx-sdk   Author: smarthall   File: util.py    MIT License 5 votes vote down vote up
def __init__(self, interval, callable, *args, **kwargs):
        threading.Thread.__init__(self)
        self.interval = interval
        self.callable = callable
        self.args = args
        self.kwargs = kwargs
        self.event = threading.Event()
        self.event.set() 
Example 63
Project: smbprotocol   Author: jborean93   File: connection.py    MIT License 5 votes vote down vote up
def __init__(self, message, message_type, connection, session_id=None):
        """
        [MS-SMB2] v53.0 2017-09-15

        3.2.1.7 Per Pending Request
        For each request that was sent to the server and is await a response
        :param message: The message to be sent in the request
        :param message_type: The type of message that is set in the header's data field.
        :param connection: The Connection the request was sent under.
        :param session_id: The Session Id the request was for.
        """
        self.async_id = None
        self.message = message
        self.timestamp = datetime.now()
        self.cancelled = False

        # Used to contain the corresponding response from the server as the receiving in done in a separate thread.
        self.response = None

        # Used by the recv processing thread to say the response has been received and is ready for consumption.
        self.response_event = threading.Event()

        # Used to lock the request when the main thread is processing the PENDING result in case the background thread
        # receives the final result and fires the event before main clears it.
        self.response_event_lock = threading.Lock()

        # Stores the message_ids of related messages that are sent in a compound request. This is only set on the 1st
        # message in the request. Used when STATUS_STOPPED_ON_SYMLINK is set and we need to send the whole compound
        # request again with the new path.
        self.related_ids = []

        # Cannot rely on the message values as it could be a related compound msg which does not set these values.
        self.session_id = session_id

        self._connection = connection
        self._message_type = message_type  # Used to rehydrate the message data in case it's needed again. 
Example 64
Project: TradzQAI   Author: kkuette   File: cbpro_wrapper.py    Apache License 2.0 5 votes vote down vote up
def buildNthread(self, n=20):
        for i in range(n):
            self.ordernthreads.append(dict(
                thread = Thread(target=self.order_managment),
                side = None,
                size = 0,
                best_price = 0,
                event = Event(),
                manager = threadsManager(authClient=self.authClient,
                    product_id=self.product_id,
                    userorder=self.orderbook.addUserOrder,
                    orders_class=self.orders),
                is_busy = False
                )
            ) 
Example 65
Project: AshsSDK   Author: thehappydinoa   File: futures.py    MIT License 5 votes vote down vote up
def __init__(self, transfer_id=None):
        self.transfer_id = transfer_id
        self._status = 'not-started'
        self._result = None
        self._exception = None
        self._associated_futures = set()
        self._failure_cleanups = []
        self._done_callbacks = []
        self._done_event = threading.Event()
        self._lock = threading.Lock()
        self._associated_futures_lock = threading.Lock()
        self._done_callbacks_lock = threading.Lock()
        self._failure_cleanups_lock = threading.Lock() 
Example 66
Project: AshsSDK   Author: thehappydinoa   File: _base.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.event = threading.Event()
        self.finished_futures = [] 
Example 67
Project: nlimb   Author: cbschaff   File: runner.py    MIT License 5 votes vote down vote up
def __init__(self, runner):
        threading.Thread.__init__(self)
        self.runner = runner
        self.daemon = True
        self._stop_event = threading.Event() 
Example 68
Project: LinguaLyrics   Author: LinguaPlayer   File: mainwindow_presenter.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, window):
        self.window = window
        self.thread_event = threading.Event()
        self.music_data = None
        self.fetched_lyric_data = None
        self.retried = False
        self.music_path = None 
Example 69
Project: Cryptchat   Author: djohsson   File: networkhandler.py    MIT License 5 votes vote down vote up
def __init__(self, address, port, servermode, dh):
        self.address = address
        self.port = port
        self.servermode = servermode
        self.dh = dh
        self.exchangedkeys = Event()
        self.starter = NetworkStarter(self)

        self.in_message = []
        self.in_condition = Condition()
        self.in_sem = Semaphore(0)

        self.out_message = []
        self.out_condition = Condition()
        self.out_sem = Semaphore(0) 
Example 70
Project: sinking   Author: Arteneko   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, worker_id, defaults, config, debug):
        super(APIService, self).__init__(worker_id=worker_id)
        self._shutdown = threading.Event()
        self._defaults = defaults
        self._config = config

        root = logging.getLogger()
        log.init(root, debug) 
Example 71
Project: sinking   Author: Arteneko   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, worker_id, defaults, config, debug):
        super(CollectorService, self).__init__(worker_id=worker_id)
        self._shutdown = threading.Event()
        self._defaults = defaults
        self._config = config

        root = logging.getLogger()
        log.init(root, debug) 
Example 72
Project: trunklucator   Author: Dumbris   File: aiothread.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        self.server = None
        super().__init__(*args, **kwargs)
        self._loop, self.event = None, Event() 
Example 73
Project: Paradrop   Author: ParadropLabs   File: manager.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, writeDir, execCommands=True):
        """
        writeDir: directory to use for generated config files (e.g. hostapd.conf).
        execCommands: whether or not to run commands (set to False for testing).
        """
        self.writeDir = writeDir
        self.execCommands = execCommands

        # Make sure directory exists.
        pdosq.makedirs(writeDir)

        self.previousCommands = list()
        self.currentConfig = dict()
        self.nextSectionId = 0

        # Number of objects requiring IP forwarding.
        # If >0, we need to enable system-wide.
        # If ==0, we can probably disable.
        self.forwardingCount = 0

        # Track whether we have loaded the conntrack kernel module.  We need to
        # make sure the module is loaded before adding iptables rules related
        # to connection state.
        self.conntrackLoaded = False

        # Allow threads to wait for first load to complete.  This will be set
        # after the first load completes and will remain set thereafter.
        self.systemUp = threading.Event()

        # Track epoch number so that we know which configuration sections
        # were applied in the most recent epoch.
        self.epoch = 0 
Example 74
Project: storjnode   Author: StorjOld   File: node.py    MIT License 5 votes vote down vote up
def test_get_unl(self):
        node_id = self.test_get_unl_peer.get_id()
        got_unl = threading.Event()

        def callback(unl):
            got_unl.set()
        self.swarm[1].get_unl_by_node_id(node_id).addCallback(callback)
        got_unl.wait(timeout=WALK_TIMEOUT * 4)
        self.assertTrue(got_unl.isSet())

    #################################
    # test util and debug functions #
    ################################# 
Example 75
Project: storjnode   Author: StorjOld   File: node.py    MIT License 5 votes vote down vote up
def test_refresh_neighbours_thread(self):
        interval = QUERY_TIMEOUT * 2
        alice_node = storjnode.network.Node(
            self.__class__.btctxstore.create_key(),
            bootstrap_nodes=[("240.0.0.0", 1337)],
            store_config={STORAGE_DIR: None},
            refresh_neighbours_interval=interval,
            nat_type="preserving",
            node_type="passive",
            disable_data_transfer=True
        )
        alice_received = threading.Event()
        alice_node.add_message_handler(lambda n, m: alice_received.set())

        bob_node = storjnode.network.Node(
            self.__class__.btctxstore.create_key(),
            bootstrap_nodes=[(LAN_IP, alice_node.port)],
            store_config={STORAGE_DIR: None},
            refresh_neighbours_interval=interval,
            nat_type="preserving",
            node_type="passive",
            disable_data_transfer=True
        )
        bob_received = threading.Event()
        bob_node.add_message_handler(lambda n, m: bob_received.set())

        time.sleep(interval * 2)  # wait until network overlay stable, 2 peers

        try:
            alice_node.relay_message(bob_node.get_id(), "hi bob")
            bob_node.relay_message(alice_node.get_id(), "hi alice")
            bob_received.wait(timeout=QUERY_TIMEOUT)
            alice_received.wait(timeout=QUERY_TIMEOUT)
            self.assertTrue(bob_received.isSet())
            self.assertTrue(alice_received.isSet())
        finally:
            alice_node.stop()
            bob_node.stop() 
Example 76
Project: pyblish-win   Author: pyblish   File: test_ssl.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def test_handshake_timeout(self):
            # Issue #5103: SSL handshake must respect the socket timeout
            server = socket.socket(socket.AF_INET)
            host = "127.0.0.1"
            port = support.bind_port(server)
            started = threading.Event()
            finish = False

            def serve():
                server.listen(5)
                started.set()
                conns = []
                while not finish:
                    r, w, e = select.select([server], [], [], 0.1)
                    if server in r:
                        # Let the socket hang around rather than having
                        # it closed by garbage collection.
                        conns.append(server.accept()[0])
                for sock in conns:
                    sock.close()

            t = threading.Thread(target=serve)
            t.start()
            started.wait()

            try:
                try:
                    c = socket.socket(socket.AF_INET)
                    c.settimeout(0.2)
                    c.connect((host, port))
                    # Will attempt handshake and time out
                    self.assertRaisesRegexp(ssl.SSLError, "timed out",
                                            ssl.wrap_socket, c)
                finally:
                    c.close()
                try:
                    c = socket.socket(socket.AF_INET)
                    c = ssl.wrap_socket(c)
                    c.settimeout(0.2)
                    # Will attempt handshake and time out
                    self.assertRaisesRegexp(ssl.SSLError, "timed out",
                                            c.connect, (host, port))
                finally:
                    c.close()
            finally:
                finish = True
                t.join()
                server.close() 
Example 77
Project: ARU-Net   Author: TobiasGruening   File: data_provider_la.py    GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, path_list_train, path_list_val, n_classes, threadNum = 24, queueCapacity=64, kwargs_dat={}):
        self.n_classes = n_classes
        self.list_train = None
        self.size_train = 0
        self.q_train = None
        self.list_val = None
        self.size_val = 0
        self.q_val = None

        self.mvn = kwargs_dat.get("mvn", False)
        self.batchsize_tr = kwargs_dat.get("batchsize_tr", 1)
        self.batchsize_val = kwargs_dat.get("batchsize_val", 1)
        self.affine_tr = kwargs_dat.get("affine_tr", False)
        self.affine_val = kwargs_dat.get("affine_val", False)
        self.affine_value = kwargs_dat.get("affine_value", 0.025)
        self.elastic_tr = kwargs_dat.get("elastic_tr", False)
        self.elastic_val = kwargs_dat.get("elastic_val", False)
        self.elastic_value_x = kwargs_dat.get("elastic_val_x", 0.0002)
        self.elastic_value_y = kwargs_dat.get("elastic_value_y", 0.0002)
        self.rotate_tr = kwargs_dat.get("rotate_tr", False)
        self.rotate_val = kwargs_dat.get("rotate_val", False)
        self.rotateMod90_tr = kwargs_dat.get("rotateMod90_tr", False)
        self.rotateMod90_val = kwargs_dat.get("rotateMod90_val", False)
        self.skelet = kwargs_dat.get("skelet", True)
        self.dilate_num = kwargs_dat.get("dilate_num", 1)
        self.scale_min = kwargs_dat.get("scale_min", 1.0)
        self.scale_max = kwargs_dat.get("scale_max", 1.0)
        self.scale_val = kwargs_dat.get("scale_val", 1.0)
        self.one_hot_encoding = kwargs_dat.get("one_hot_encoding", True)
        self.dominating_channel = kwargs_dat.get("dominating_channel", 0)
        self.dominating_channel = min(self.dominating_channel, n_classes-1)
        self.shuffle = kwargs_dat.get("shuffle", True)

        self.threadNum = threadNum
        self.queueCapacity = queueCapacity
        self.stopTrain = threading.Event()
        self.stopVal = threading.Event()
        if path_list_train != None:
            self.list_train = read_image_list(path_list_train)
            self.size_train = len(self.list_train)
            self.q_train, self.threads_tr = self._get_list_queue(self.list_train, self.threadNum, self.queueCapacity, self.stopTrain, self.batchsize_tr,
                                                self.scale_min, self.scale_max, self.affine_tr, self.elastic_tr, self.rotate_tr, self.rotateMod90_tr)
        if path_list_val != None:
            self.list_val = read_image_list(path_list_val)
            self.size_val = len(self.list_val)
            self.q_val, self.threads_val = self._get_list_queue(self.list_val, 1, 100, self.stopVal, self.batchsize_val, self.scale_val, self.scale_val, self.affine_val, self.elastic_val, self.rotate_val, self.rotateMod90_val) 
Example 78
Project: python-lifx-sdk   Author: smarthall   File: device.py    MIT License 4 votes vote down vote up
def _block_for(self, need_ack, need_res, *args, **kwargs):
        """
        Send a packet and block waiting for the replies.

        Only needs the type and an optional payload.
        """
        if need_ack and need_res:
            raise NotImplemented('Waiting for both acknowledgement and response not yet supported.')

        sequence = self._seq
        timeout = kwargs.get('timeout', DEFAULT_TIMEOUT)
        sub_timeout = timeout / DEFAULT_RETRANSMITS

        for i in range(1, DEFAULT_RETRANSMITS):
            if i != 1:
                self._dropped_packets += 1

            e = Event()
            self._tracked[sequence] = e

            self._send_packet(
                    ack_required=need_ack,
                    res_required=need_res,
                    sequence=sequence,
                    *args,
                    **kwargs
            )

            # If we don't care about a response, don't block at all
            if not (need_ack or need_res):
                return None

            if e.wait(sub_timeout):
                response = self._responses[sequence]

                # TODO: Check if the response was actually what we expected

                if need_res:
                    return response.payload
                else:
                    return True

        # We did get a response
        raise DeviceTimeoutError(self, timeout, DEFAULT_RETRANSMITS) 
Example 79
Project: TradzQAI   Author: kkuette   File: cbpro_wrapper.py    Apache License 2.0 4 votes vote down vote up
def __init__(self,
                 key=None,
                 b64=None,
                 passphrase=None,
                 url="https://api.pro.coinbase.com",
                 product_id=['BTC-EUR'],
                 db=None,
                 max_orders=5,
                 max_request_per_sec=5,
                 mode="maker",
                 auto_cancel=True):

        self.key = key
        self.b64 = b64
        self.passphrase = passphrase
        self.url = url
        self.product_id = product_id
        self.auto_cancel = auto_cancel
        self.db = db
        self.authClient = None
        self.mode = mode
        self.maxthreads = max_orders
        self.maxRequestPerSec = max_request_per_sec
        self.spread = 0
        self.orders = orders()
        self.last_bids = None
        self.last_asks = None
        self.ordernthreads = deque(maxlen=self.maxthreads)
        self.lasto_thread = 0
        self.oncthread = 0
        self.selector_id = 0
        self.unused_manager = 0
        self.no_thread_used = False
        self.order_size = 0
        self.is_running = False
        self.is_done = True
        self.bpfunc = None
        self.bestprice = 0
        self.requestTime = time.time()
        self.event = Event()
        self.event.clear()
        self.connectOrderBook()
        self.connectAuthClient()
        self.setBestPriceFunc(self.getBestPrice) 
Example 80
Project: TradzQAI   Author: kkuette   File: base_env.py    Apache License 2.0 4 votes vote down vote up
def __init__(self):

        self.current_step = dict(
            episode = 0,
            step = -1
        )

        self.date = dict(
            day = 1,
            month = 1,
            year = 1,
            total_minutes = 1,
            total_day = 1,
            total_month = 1,
            total_year = 1
        )

        self.actions = 3
        self.step_left = 0
        self.api = None
        self.pause = 0
        self.action = None
        self.start_t = 0
        self.loop_t = 0
        self._api = False
        self.day_changed = False
        self.closed = False
        self.stop = False
        self.event = Event()
        self.event.clear()

        self.lst_act = deque(maxlen=1000)
        self.lst_reward = deque(maxlen=1000)
        self.lst_state = deque(maxlen=1000)

        self.train_in = []
        self.train_out = []

        self.time = None
        self.lst_data_full = deque(maxlen=100)
        self.lst_data_preprocessed = []
        self.offset = 0

        self.historical = []
        self.input_names = []

        self.tensorOCHL = [[] for _ in range(4)]