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 |
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: inst.py From kaldi-python-io with Apache License 2.0 | 9 votes |
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 #3
Source File: from_chinastock.py From Financial-NLP with Apache License 2.0 | 8 votes |
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 #4
Source File: slowDown.py From Learning-Concurrency-in-Python with MIT License | 8 votes |
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 |
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: from_chinastock.py From Financial-NLP with Apache License 2.0 | 6 votes |
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 #7
Source File: test_orch_generic.py From drydock with Apache License 2.0 | 6 votes |
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: chapter9_9.py From Mastering-Python-Networking-Second-Edition with MIT License | 6 votes |
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 #9
Source File: chapter9_8.py From Mastering-Python-Networking-Second-Edition with MIT License | 6 votes |
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 #10
Source File: receive.py From RF-Monitor with GNU General Public License v2.0 | 6 votes |
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 #11
Source File: wspbus.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #12
Source File: servers.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #13
Source File: test_bus.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #14
Source File: test_session.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #15
Source File: producerConsumer.py From Learning-Concurrency-in-Python with MIT License | 6 votes |
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 #16
Source File: route_server.py From iSDX with Apache License 2.0 | 6 votes |
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 #17
Source File: participant_controller.py From iSDX with Apache License 2.0 | 6 votes |
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 #18
Source File: replay.py From iSDX with Apache License 2.0 | 6 votes |
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 #19
Source File: Logger.py From StructEngPy with MIT License | 6 votes |
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 #20
Source File: paint.py From unicorn-hat-hd with MIT License | 5 votes |
def clear(): s = threading.Thread(None,unicornhathd.clear) s.start() return "ok"
Example #21
Source File: paint.py From unicorn-hat-hd with MIT License | 5 votes |
def show(): s = threading.Thread(None,unicornhathd.show) s.start() return "ok"
Example #22
Source File: train.py From cat-bbs with MIT License | 5 votes |
def __init__(self, load_batch_func, nb_workers=1, queue_size=50, threaded=True): self.queue = multiprocessing.Queue(queue_size) self.workers = [] for i in range(nb_workers): if threaded: worker = threading.Thread(target=self._load_batches, args=(load_batch_func, self.queue)) else: worker = multiprocessing.Process(target=self._load_batches, args=(load_batch_func, self.queue)) worker.daemon = True worker.start() self.workers.append(worker)
Example #23
Source File: train.py From cat-bbs with MIT License | 5 votes |
def __init__(self, augseq, queue_source, nb_workers, queue_size=50, threaded=False): assert 0 < queue_size <= 10000 self.augseq = augseq self.queue_source = queue_source self.queue_result = multiprocessing.Queue(queue_size) self.workers = [] for i in range(nb_workers): augseq.reseed() if threaded: worker = threading.Thread(target=self._augment_images_worker, args=(self.augseq, self.queue_source, self.queue_result)) else: worker = multiprocessing.Process(target=self._augment_images_worker, args=(self.augseq, self.queue_source, self.queue_result)) worker.daemon = True worker.start() self.workers.append(worker)
Example #24
Source File: test_orch_generic.py From drydock with Apache License 2.0 | 5 votes |
def test_task_termination(self, input_files, deckhand_ingester, setup, blank_state): 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(2) orchestrator.terminate_task(orch_task) time.sleep(10) orch_task = blank_state.get_task(orch_task.get_id()) assert orch_task.get_status() == hd_fields.TaskStatus.Terminated finally: orchestrator.stop_orchestrator() orch_thread.join(10)
Example #25
Source File: chapter9_9.py From Mastering-Python-Networking-Second-Edition with MIT License | 5 votes |
def background(f): """Decorator that runs the wrapped function as a background task. It is assumed that this function creates a new resource, and takes a long time to do so. The response has status code 202 Accepted and includes a Location header with the URL of a task resource. Sending a GET request to the task will continue to return 202 for as long as the task is running. When the task has finished, a status code 303 See Other will be returned, along with a Location header that points to the newly created resource. The client then needs to send a DELETE request to the task resource to remove it from the system.""" @functools.wraps(f) def wrapped(*args, **kwargs): # The background task needs to be decorated with Flask's # copy_current_request_context to have access to context globals. @copy_current_request_context def task(): global background_tasks try: # invoke the wrapped function and record the returned # response in the background_tasks dictionary background_tasks[id] = make_response(f(*args, **kwargs)) except: # the wrapped function raised an exception, return a 500 # response background_tasks[id] = make_response(internal_server_error()) # store the background task under a randomly generated identifier # and start it global background_tasks id = uuid.uuid4().hex background_tasks[id] = Thread(target=task) background_tasks[id].start() # return a 202 Accepted response with the location of the task status # resource return jsonify({}), 202, {'Location': url_for('get_task_status', id=id)} return wrapped # g is the context request object from Flask
Example #26
Source File: chapter9_8.py From Mastering-Python-Networking-Second-Edition with MIT License | 5 votes |
def background(f): """Decorator that runs the wrapped function as a background task. It is assumed that this function creates a new resource, and takes a long time to do so. The response has status code 202 Accepted and includes a Location header with the URL of a task resource. Sending a GET request to the task will continue to return 202 for as long as the task is running. When the task has finished, a status code 303 See Other will be returned, along with a Location header that points to the newly created resource. The client then needs to send a DELETE request to the task resource to remove it from the system.""" @functools.wraps(f) def wrapped(*args, **kwargs): # The background task needs to be decorated with Flask's # copy_current_request_context to have access to context globals. @copy_current_request_context def task(): global background_tasks try: # invoke the wrapped function and record the returned # response in the background_tasks dictionary background_tasks[id] = make_response(f(*args, **kwargs)) except: # the wrapped function raised an exception, return a 500 # response background_tasks[id] = make_response(internal_server_error()) # store the background task under a randomly generated identifier # and start it global background_tasks id = uuid.uuid4().hex background_tasks[id] = Thread(target=task) background_tasks[id].start() # return a 202 Accepted response with the location of the task status # resource return jsonify({}), 202, {'Location': url_for('get_task_status', id=id)} return wrapped
Example #27
Source File: push.py From RF-Monitor with GNU General Public License v2.0 | 5 votes |
def send(self, uri, data): thread = threading.Thread(target=self.__send, args=(uri, data,)) thread.daemon = True thread.start()
Example #28
Source File: gps.py From RF-Monitor with GNU General Public License v2.0 | 5 votes |
def __init__(self, eventHandler, gpsDevice): threading.Thread.__init__(self) self.name = 'GPS' self._gpsDevice = gpsDevice self._eventHandler = eventHandler self._comm = None self._timeout = None self._cancel = False self._sats = {} self.start()
Example #29
Source File: gps.py From RF-Monitor with GNU General Public License v2.0 | 5 votes |
def __init__(self, callback): threading.Thread.__init__(self) self.name = 'GPS Timeout' self._callback = callback self._done = threading.Event() self._reset = True self.start()
Example #30
Source File: decorators.py From pkmeter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __get__(self, inst, owner): if inst is None: return self key = self._func.__get__(inst, type(inst)) if key not in self._queues: self._queues[key] = queue.Queue() self._threads[key] = threading.Thread(target=self._thread_loop, args=[inst, key]) self._threads[key].daemon = True self._threads[key].start() return lambda *a, **k: self._queues[key].put((a, k))