Python threading.Thread() Examples

The following are 30 code examples of threading.Thread(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module threading , or try the search function .
Example #1
Source File: server.py    From RF-Monitor with GNU General Public License v2.0 10 votes vote down vote up
def __init__(self, eventHandler):
        threading.Thread.__init__(self)
        self.name = 'Server'
        self.daemon = True
        self._eventHandler = eventHandler

        self._client = None
        self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        try:
            self._server.bind(('', PORT))
            self._server.listen(5)
        except socket.error:
            event = Event(Events.SCAN_ERROR, msg='Could not start server')
            post_event(eventHandler, event)
            return

        self._cancel = False
        self.start() 
Example #2
Source File: from_chinastock.py    From Financial-NLP with Apache License 2.0 9 votes vote down vote up
def download_all_section(*arg):
    if len(arg)==1:
        k=arg[0]
        th=[]
        for key in decode.keys():
            th.append(threading.Thread(target=download, args=(key,k)))
        for t in th:
            t.start()
        for t in th:
            t.join()      
    elif len(arg)==2:
        From=arg[0]
        To=arg[1]
        th=[]
        for key in decode.keys():
            th.append(threading.Thread(target=download, args=(key, From, To)))
        for t in th:
            t.start()
        for t in th:
            t.join() 
Example #3
Source File: inst.py    From kaldi-python-io with Apache License 2.0 9 votes vote down vote up
def pipe_fopen(command, mode, background=True):
    if mode not in ["rb", "r"]:
        raise RuntimeError("Now only support input from pipe")

    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

    def background_command_waiter(command, p):
        p.wait()
        if p.returncode != 0:
            warnings.warn("Command \"{0}\" exited with status {1}".format(
                command, p.returncode))
            _thread.interrupt_main()

    if background:
        thread = threading.Thread(target=background_command_waiter,
                                  args=(command, p))
        # exits abnormally if main thread is terminated .
        thread.daemon = True
        thread.start()
    else:
        background_command_waiter(command, p)
    return p.stdout 
Example #4
Source File: slowDown.py    From Learning-Concurrency-in-Python with MIT License 8 votes vote down vote up
def main():
  print("Starting number crunching")
  t0 = time.time()
  
  threads = []

  for i in range(10):
    thread = threading.Thread(target=executeProc)
    threads.append(thread)
    thread.start()

  for thread in threads:
    thread.join()

  t1 = time.time()
  totalTime = t1 - t0
  print("Execution Time: {}".format(totalTime)) 
Example #5
Source File: progress_indicator.py    From clikit with MIT License 7 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.write_line("")

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

            raise

        self.finish(end_message, reset_indicator=True) 
Example #6
Source File: test_session.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_8_Ram_Cleanup(self):
        def lock():
            s1 = sessions.RamSession()
            s1.acquire_lock()
            time.sleep(1)
            s1.release_lock()

        t = threading.Thread(target=lock)
        t.start()
        start = time.time()
        while not sessions.RamSession.locks and time.time() - start < 5:
            time.sleep(0.01)
        assert len(sessions.RamSession.locks) == 1, 'Lock not acquired'
        s2 = sessions.RamSession()
        s2.clean_up()
        msg = 'Clean up should not remove active lock'
        assert len(sessions.RamSession.locks) == 1, msg
        t.join() 
Example #7
Source File: test_orch_generic.py    From drydock with Apache License 2.0 6 votes vote down vote up
def test_task_complete(self, deckhand_ingester, input_files, setup,
                           blank_state, mock_get_build_data):
        input_file = input_files.join("deckhand_fullsite.yaml")
        design_ref = "file://%s" % str(input_file)

        orchestrator = orch.Orchestrator(
            state_manager=blank_state, ingester=deckhand_ingester)
        orch_task = orchestrator.create_task(
            action=hd_fields.OrchestratorAction.Noop, design_ref=design_ref)
        orch_task.set_status(hd_fields.TaskStatus.Queued)
        orch_task.save()

        orch_thread = threading.Thread(target=orchestrator.watch_for_tasks)
        orch_thread.start()

        try:
            time.sleep(10)

            orch_task = blank_state.get_task(orch_task.get_id())

            assert orch_task.get_status() == hd_fields.TaskStatus.Complete
        finally:
            orchestrator.stop_orchestrator()
            orch_thread.join(10) 
Example #8
Source File: Logger.py    From StructEngPy with MIT License 6 votes vote down vote up
def info(log:str,target='console'):
    """
    log: text to record.
    target: 'console' to print log on screen or file to write in. 
    """
    if target=='console':
        thd=threading.Thread(target=print,args=(ctime(),':',log))
        thd.setDaemon(True)
        thd.start()
        thd.join()
    else:
        try:
            thd=threading.Thread(target=print,args=(ctime(),':',log))
            thd.setDaemon(True)
            thd.start()
            thd.join()
        except Exception as e:
            print(e) 
Example #9
Source File: from_chinastock.py    From Financial-NLP with Apache License 2.0 6 votes vote down vote up
def parallel_download_all_section(*arg):
    if len(arg)==1:
        k=arg[0]
        pro=[]
        for key in decode.keys():
            pro.append(multiprocessing.Process(target=download, args=(key, k)))
            #th.append(threading.Thread(target=download, args=(key,k)))
        for p in pro:
            p.start()
        for p in pro:
            p.join()
    elif len(arg)==2:
        From=arg[0]
        To=arg[1]
        pro=[]
        for key in decode.keys():
            pro.append(multiprocessing.Process(target=download, args=(key, From, To)))
            #th.append(threading.Thread(target=download, args=(key,k)))
        for p in pro:
            p.start()
        for p in pro:
            p.join() 
Example #10
Source File: chapter9_9.py    From Mastering-Python-Networking-Second-Edition with MIT License 6 votes vote down vote up
def get_task_status(id):
    """Query the status of an asynchronous task."""
    # obtain the task and validate it
    global background_tasks
    rv = background_tasks.get(id)
    if rv is None:
        return not_found(None)

    # if the task object is a Thread object that means that the task is still
    # running. In this case return the 202 status message again.
    if isinstance(rv, Thread):
        return jsonify({}), 202, {'Location': url_for('get_task_status', id=id)}

    # If the task object is not a Thread then it is assumed to be the response
    # of the finished task, so that is the response that is returned.
    # If the application is configured to auto-delete task status resources once
    # the task is done then the deletion happens now, if not the client is
    # expected to send a delete request.
    if app.config['AUTO_DELETE_BG_TASKS']:
        del background_tasks[id]
    return rv 
Example #11
Source File: chapter9_8.py    From Mastering-Python-Networking-Second-Edition with MIT License 6 votes vote down vote up
def get_task_status(id):
    """Query the status of an asynchronous task."""
    # obtain the task and validate it
    global background_tasks
    rv = background_tasks.get(id)
    if rv is None:
        return not_found(None)

    # if the task object is a Thread object that means that the task is still
    # running. In this case return the 202 status message again.
    if isinstance(rv, Thread):
        return jsonify({}), 202, {'Location': url_for('get_task_status', id=id)}

    # If the task object is not a Thread then it is assumed to be the response
    # of the finished task, so that is the response that is returned.
    # If the application is configured to auto-delete task status resources once
    # the task is done then the deletion happens now, if not the client is
    # expected to send a delete request.
    if app.config['AUTO_DELETE_BG_TASKS']:
        del background_tasks[id]
    return rv 
Example #12
Source File: replay.py    From iSDX with Apache License 2.0 6 votes vote down vote up
def main(argv):
    logging.basicConfig(level=logging.INFO)

    log_history = LogHistory(argv.config, argv.flow_dir, argv.port_dir, int(argv.num_steps), debug=True)

    channel = "sdx_stats"
    address = "192.168.99.100"
    port = 6379
    db = 0

    publisher = Publisher(channel, address, port)

    log_replay = LogReplay(log_history, publisher, int(argv.timestep), debug=True)

    # start replay
    replay_thread = Thread(target=log_replay.start)
    replay_thread.daemon = True
    replay_thread.start()

    while replay_thread.is_alive():
        try:
            replay_thread.join(1)
        except KeyboardInterrupt:
            log_replay.stop() 
Example #13
Source File: route_server.py    From iSDX with Apache License 2.0 6 votes vote down vote up
def start(self):
        logger.info("Starting the BGP PctrlListener")

        while self.run:
            conn = self.listener.accept()

            pc = PctrlClient(conn, self.listener.last_accepted)
            t = Thread(target=pc.start)

            with clientPoolLock:
                logger.debug('Trace: PctrlListener.start: clientActivePool before: %s', clientActivePool)
                logger.debug('Trace: PctrlListener.start: clientDeadPool before: %s', clientDeadPool)
                clientActivePool[pc] = t

                # while here, join dead threads.
                while clientDeadPool:
                    clientDeadPool.pop().join()
                logger.debug('Trace: PctrlListener.start: clientActivePool after: %s', clientActivePool)
                logger.debug('Trace: PctrlListener.start: clientDeadPool after: %s', clientDeadPool)

            t.start() 
Example #14
Source File: wspbus.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def start_with_callback(self, func, args=None, kwargs=None):
        """Start 'func' in a new thread T, then start self (and return T)."""
        if args is None:
            args = ()
        if kwargs is None:
            kwargs = {}
        args = (func,) + args

        def _callback(func, *a, **kw):
            self.wait(states.STARTED)
            func(*a, **kw)
        t = threading.Thread(target=_callback, args=args, kwargs=kwargs)
        t.setName('Bus Callback ' + t.getName())
        t.start()

        self.start()

        return t 
Example #15
Source File: servers.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def start(self):
        """Start the HTTP server."""
        if self.running:
            self.bus.log('Already serving on %s' % self.description)
            return

        self.interrupt = None
        if not self.httpserver:
            raise ValueError('No HTTP server has been created.')

        if not os.environ.get('LISTEN_PID', None):
            # Start the httpserver in a new thread.
            if isinstance(self.bind_addr, tuple):
                portend.free(*self.bind_addr, timeout=Timeouts.free)

        import threading
        t = threading.Thread(target=self._start_http_thread)
        t.setName('HTTPServer ' + t.getName())
        t.start()

        self.wait()
        self.running = True
        self.bus.log('Serving on %s' % self.description) 
Example #16
Source File: producerConsumer.py    From Learning-Concurrency-in-Python with MIT License 6 votes vote down vote up
def run(self):
        """
        Thread run method. Consumes integers from list
        """
        while True:
            self.condition.acquire()
            print 'condition acquired by %s' % self.name
            while True:
                if self.integers:
                    integer = self.integers.pop()
                    print '%d popped from list by %s' % (integer, self.name)
                    break
                print 'condition wait by %s' % self.name
                self.condition.wait()
            print 'condition released by %s' % self.name
            self.condition.release() 
Example #17
Source File: receive.py    From RF-Monitor with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, eventHandler, freq, gain, cal):
        threading.Thread.__init__(self)
        self.name = 'Receive'
        self.daemon = True

        self._cancel = False
        self._freq = freq
        self._gain = gain
        self._cal = cal
        self._eventHandler = eventHandler
        self._sdr = None
        self._capture = (ctypes.c_ubyte * SAMPLES)()

        devices = rtlsdr.librtlsdr.rtlsdr_get_device_count()
        if devices == 0:
            event = Event(Events.SCAN_ERROR, msg='No device found')
            post_event(eventHandler, event)
        else:
            self.start() 
Example #18
Source File: test_bus.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_wait(bus):
    """Test that bus wait awaits for states."""
    def f(method):  # pylint: disable=invalid-name
        time.sleep(0.2)
        getattr(bus, method)()

    flow = [
        ('start', [bus.states.STARTED]),
        ('stop', [bus.states.STOPPED]),
        ('start', [bus.states.STARTING, bus.states.STARTED]),
        ('exit', [bus.states.EXITING]),
    ]

    for method, states in flow:
        threading.Thread(target=f, args=(method,)).start()
        bus.wait(states)

        # The wait method MUST wait for the given state(s).
        assert bus.state in states, 'State %r not in %r' % (bus.state, states) 
Example #19
Source File: participant_controller.py    From iSDX with Apache License 2.0 6 votes vote down vote up
def start_eh_arp(self):
        self.logger.info("ARP Event Handler started.")

        while self.run:
            # need to poll since recv() will not detect close from this end
            # and need some way to shutdown gracefully.
            if not self.arp_client.poll(1):
                continue
            try:
                tmp = self.arp_client.recv()
            except EOFError:
                break

            data = json.loads(tmp)
            self.logger.debug("ARP Event received: %s", data)

            # Starting a thread for independently processing each incoming network event
            event_processor_thread = Thread(target=self.process_event, args=(data,))
            event_processor_thread.daemon = True
            event_processor_thread.start()

        self.arp_client.close()
        self.logger.debug("Exiting start_eh_arp") 
Example #20
Source File: diningPhilosophers.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def __init__(self, name, leftFork, rightFork):
    print("{} Has Sat Down At the Table".format(name))
    threading.Thread.__init__(self, name=name)
    self.leftFork = leftFork
    self.rightFork = rightFork 
Example #21
Source File: taskSheduler.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def main():
  t0 = time.time()
  thread1 = threading.Thread(target=workerA)
  thread2 = threading.Thread(target=workerB)

  thread1.start()
  thread2.start()

  thread1.join()
  thread2.join()

  t1 = time.time()

  print("Execution Time {}".format(t1-t0)) 
Example #22
Source File: barriers.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def run(self):
      print("Thread {} working on something".format(threading.current_thread()))
      time.sleep(random.randint(1,10))
      print("Thread {} is joining {} waiting on Barrier".format(threading.current_thread(), self.barrier.n_waiting))
      self.barrier.wait()
      
      print("Barrier has been lifted, continuing with work") 
Example #23
Source File: threadJoin.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def ourThread(i):
  print("Thread {} Started".format(i))
  time.sleep(i*2)
  print("Thread {} Finished".format(i)) 
Example #24
Source File: server.py    From iSDX with Apache License 2.0 5 votes vote down vote up
def start(self):
        self.conn = self.listener.accept()
        self.logger.debug('Connection accepted from '+str(self.listener.last_accepted))

        self.sender = Thread(target=_sender, args=(self.conn,self.sender_queue))
        self.sender.start()

        self.receiver = Thread(target=_receiver, args=(self.conn,self.receiver_queue))
        self.receiver.start() 
Example #25
Source File: threadException.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def myThread(queue):
  while True:
    try:
      time.sleep(2)
      raise Exception("Exception Thrown In Child Thread {}".format(threading.current_thread()))
    except:
      queue.put(sys.exc_info()) 
Example #26
Source File: threadJoin.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def main():
  thread = threading.Thread(target=ourThread, args=(1,))
  thread.start()

  print("Is thread 1 Finished?")

  thread2 = threading.Thread(target=ourThread, args=(2,))
  thread2.start()
  thread2.join()

  print("Thread 2 definitely finished") 
Example #27
Source File: webCrawler.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def __init__(self, baseUrl, linksToCrawl, haveVisited, errorLinks, urlLock):
    threading.Thread.__init__(self);
    print("Web Crawler Worker Started: {}".format(threading.current_thread()))
    self.linksToCrawl = linksToCrawl
    self.haveVisited = haveVisited
    self.baseUrl = baseUrl
    self.urlLock = urlLock
    self.errorLinks = errorLinks 
Example #28
Source File: pubSub.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def __init__(self, integers, condition):
    self.condition = condition
    self.integers = integers
    threading.Thread.__init__(self) 
Example #29
Source File: pubSub.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def __init__(self, integers, condition):
    self.integers = integers
    self.condition = condition
    threading.Thread.__init__(self) 
Example #30
Source File: mainThread.py    From Learning-Concurrency-in-Python with MIT License 5 votes vote down vote up
def myChildThread():
  print("Child Thread Starting")
  time.sleep(5)
  print("Current Thread ----------")
  print(threading.current_thread())
  print("-------------------------")
  print("Main Thread -------------")
  print(threading.main_thread())
  print("-------------------------")
  print("Child Thread Ending")