def simulated_time_sleep(secs=None): if secs is None: # Thread just wants to yield to any other waiting thread. self.give_way() return # Create a new queue. queue = eventlet.Queue(1) queue.stack = inspect.stack()[1][3] # Add it to the dict of sleepers, together with the waking up time. self.sleepers[queue] = self.current_time + secs _log.info("T=%s: %s: Start sleep for %ss until T=%s", self.current_time, queue.stack, secs, self.sleepers[queue]) # Do a zero time real sleep, to allow other threads to run. self.real_eventlet_sleep(REAL_EVENTLET_SLEEP_TIME) # Block until something is posted to the queue. queue.get(True) # Wake up. return None
def scatter_gather_cells(context, cell_mappings, timeout, fn, *args, **kwargs): """Target cells in parallel and return their results. The first parameter in the signature of the function to call for each cell should be of type RequestContext. :param context: The RequestContext for querying cells :param cell_mappings: The CellMappings to target in parallel :param timeout: The total time in seconds to wait for all the results to be gathered :param fn: The function to call for each cell :param args: The args for the function to call for each cell, not including the RequestContext :param kwargs: The kwargs for the function to call for each cell :returns: A dict {cell_uuid: result} containing the joined results. The did_not_respond_sentinel will be returned if a cell did not respond within the timeout. The raised_exception_sentinel will be returned if the call to a cell raised an exception. The exception will be logged. """ greenthreads = [] queue = eventlet.queue.LightQueue() results = {} def gather_result(cell_uuid, fn, *args, **kwargs): try: result = fn(*args, **kwargs) except Exception: LOG.exception('Error gathering result from cell %s', cell_uuid) result = raised_exception_sentinel # The queue is already synchronized. queue.put((cell_uuid, result)) for cell_mapping in cell_mappings: with target_cell(context, cell_mapping) as cctxt: greenthreads.append((cell_mapping.uuid, utils.spawn(gather_result, cell_mapping.uuid, fn, cctxt, *args, **kwargs))) with eventlet.timeout.Timeout(timeout, exception.CellTimeout): try: while len(results) != len(greenthreads): cell_uuid, result = queue.get() results[cell_uuid] = result except exception.CellTimeout: # NOTE(melwitt): We'll fill in did_not_respond_sentinels at the # same time we kill/wait for the green threads. pass # Kill the green threads still pending and wait on those we know are done. for cell_uuid, greenthread in greenthreads: if cell_uuid not in results: greenthread.kill() results[cell_uuid] = did_not_respond_sentinel LOG.warning('Timed out waiting for response from cell %s', cell_uuid) else: greenthread.wait() return results
def simulated_time_sleep(secs): # Create a new queue. queue = eventlet.Queue(1) queue.stack = inspect.stack()[1][3] # Add it to the dict of sleepers, together with the waking up time. self.sleepers[queue] = self.current_time + secs print("T=%s: %s: Start sleep for %ss until T=%s" % ( self.current_time, queue.stack, secs, self.sleepers[queue] )) # Do a zero time real sleep, to allow other threads to run. self.real_eventlet_sleep(REAL_EVENTLET_SLEEP_TIME) # Block until something is posted to the queue. queue.get(True) # Wake up. return None
def wrapper(*args, **kwargs): greenthreads = [] responded = [] results = [] queue = eventlet.queue.LightQueue() @wraps(func) def parallel_task(i, *ars, **kwars): try: result = func(*ars, **kwars) if not isinstance(result, Iterable): result = [result] queue.put((i, result)) except Exception: pass workload = args[0] for i, load in enumerate(split_workload(num_workers, workload)): ar = [load] ar += args[1:] greenthreads.append( (i, eventlet.spawn(parallel_task, i, *ar, **kwargs))) with eventlet.timeout.Timeout(timeout, exception.ParallelTimeout): try: while len(responded) < len(greenthreads): if max_results != -1 and max_results <= len(results): LOG.info("Max results %s, gathered for %s", max_results, func.__name__) break i, result = queue.get() responded.append(i) results += result except exception.ParallelTimeout: LOG.error("Timeout for parallel task exceeded") pass for id_, greenthread in greenthreads: if id_ not in responded: greenthread.kill() else: greenthread.wait() return results
def simulated_time_sleep(secs): # Create a new queue. queue = eventlet.Queue(1) queue.stack = inspect.stack()[1][3] # Add it to the dict of sleepers, together with the waking up time. sleepers[queue] = current_time + secs print "T=%s: %s: Start sleep for %ss until T=%s" % ( current_time, queue.stack, secs, sleepers[queue]) # Do a zero time real sleep, to allow other threads to run. real_eventlet_sleep(REAL_EVENTLET_SLEEP_TIME) # Block until something is posted to the queue. ignored = queue.get(True) # Wake up. return None
def _iter_queue(self, queue, block): while True: try: yield queue.get(block=block) except eventlet.queue.Empty: break
def fetch_item(): got.append(queue.get())