本文整理汇总了Python中six.moves.queue.Queue.put方法的典型用法代码示例。如果您正苦于以下问题:Python Queue.put方法的具体用法?Python Queue.put怎么用?Python Queue.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.queue.Queue
的用法示例。
在下文中一共展示了Queue.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ThreadTestCase
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class ThreadTestCase(testtools.TestCase):
def setUp(self):
super(ThreadTestCase, self).setUp()
self.got_items = Queue()
self.got_args_kwargs = Queue()
self.starting_thread_count = threading.active_count()
def _func(self, conn, item, *args, **kwargs):
self.got_items.put((conn, item))
self.got_args_kwargs.put((args, kwargs))
if item == 'sleep':
sleep(1)
if item == 'go boom':
raise Exception('I went boom!')
return 'success'
def _create_conn(self):
return "This is a connection"
def _create_conn_fail(self):
raise Exception("This is a failed connection")
def assertQueueContains(self, queue, expected_contents):
got_contents = []
try:
while True:
got_contents.append(queue.get(timeout=0.1))
except Empty:
pass
if isinstance(expected_contents, set):
got_contents = set(got_contents)
self.assertEqual(expected_contents, got_contents)
示例2: run
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def run(self):
args = list(islice(self.reqs, self.requests))
if self.shuffle:
random.shuffle(args)
print("Total requests: %d" % len(args))
print("Concurrency : %d" % self.concurrency)
starttime = time.time()
q, p = Queue(), Queue()
for _ in six.moves.range(self.concurrency):
t = Thread(target=worker, args=(self.host, q, p, self.verbose))
t.daemon = True
t.start()
for a in args:
q.put(a)
q.join()
outputs = []
for _ in six.moves.range(self.requests):
outputs.append(p.get())
elapsed = time.time() - starttime
print()
print("Total requests: %d" % len(args))
print("Concurrency : %d" % self.concurrency)
print("Elapsed time : %.3fs" % elapsed)
print("Avg time p/req: %.3fs" % (elapsed / len(args)))
print("Received (per status code or error):")
for c, n in Counter(outputs).items():
print(" %s: %d" % (c, n))
示例3: test_acquire_contextmanager
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def test_acquire_contextmanager(self):
class TestedClass(Monitor):
def __init__(self, cqueue):
self.cqueue = cqueue
Monitor.__init__(self)
@Monitor.synchronized
def execute(self):
self.cqueue.put(1)
sleep(1)
self.cqueue.get()
class TesterThread(Thread):
def __init__(self, tc):
self.tc = tc
Thread.__init__(self)
def run(self):
self.tc.execute()
cq = Queue()
cq.put(1)
tc = TestedClass(cq)
tt = TesterThread(tc)
with Monitor.acquire(tc):
tt.start()
sleep(0.4)
self.assertEqual(cq.qsize(), 1)
示例4: _SBPQueueIterator
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class _SBPQueueIterator(six.Iterator):
"""
Class for upstream iterators. Implements callable interface for adding
messages into the queue, and iterable interface for getting them out.
"""
def __init__(self, maxsize):
self._queue = Queue(maxsize)
self._broken = False
def __iter__(self):
return self
def __call__(self, msg, **metadata):
self._queue.put((msg, metadata), False)
def breakiter(self):
self._broken = True
self._queue.put(None, True, 1.0)
def __next__(self):
if self._broken and self._queue.empty():
raise StopIteration
m = self._queue.get(True)
if self._broken and m is None:
raise StopIteration
return m
示例5: ThreadTestCase
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class ThreadTestCase(testtools.TestCase):
def setUp(self):
super(ThreadTestCase, self).setUp()
self.got_args_kwargs = Queue()
self.starting_thread_count = threading.active_count()
def _func(self, q_item, *args, **kwargs):
self.got_items.put(q_item)
self.got_args_kwargs.put((args, kwargs))
if q_item == 'go boom':
raise Exception('I went boom!')
if q_item == 'c boom':
raise ClientException(
'Client Boom', http_scheme='http', http_host='192.168.22.1',
http_port=80, http_path='/booze', http_status=404,
http_reason='to much', http_response_content='no sir!')
return 'best result EVAR!'
def assertQueueContains(self, queue, expected_contents):
got_contents = []
try:
while True:
got_contents.append(queue.get(timeout=0.1))
except Empty:
pass
if isinstance(expected_contents, set):
got_contents = set(got_contents)
self.assertEqual(expected_contents, got_contents)
示例6: lines
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def lines(self, fuseki_process):
"""
Provides an iterator generating the encoded string representation
of each member of this metarelate mapping translation.
Returns:
An iterator of string.
"""
msg = '\tGenerating phenomenon translation {!r}.'
print(msg.format(self.mapping_name))
lines = ['\n%s = {\n' % self.mapping_name]
# Retrieve encodings for the collection of mapping instances.
# Retrieval is threaded as it is heavily bound by resource resolution
# over http.
# Queue for metarelate mapping instances
mapenc_queue = Queue()
for mapping in self.mappings:
mapenc_queue.put(mapping)
# deque to contain the results of the jobs processed from the queue
mapencs = deque()
# run worker threads
for i in range(MAXTHREADS):
MappingEncodeWorker(mapenc_queue, mapencs, fuseki_process).start()
# block progress until the queue is empty
mapenc_queue.join()
# end of threaded retrieval process.
# now sort the payload
payload = [mapenc.encoding for mapenc in mapencs]
payload.sort(key=self._key)
lines.extend(payload)
lines.append(' }\n')
return iter(lines)
示例7: FrameSaver
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class FrameSaver( threading.Thread ):
def __init__( self ):
threading.Thread.__init__( self )
self.daemon = True
self.name = 'FrameSaver'
self.reset()
def reset( self ):
self.queue = Queue()
def run( self ):
self.reset()
while 1:
message = self.queue.get()
if message[0] == 'Save':
cmd, fileName, bib, t, frame = message
#sys.stderr.write( 'save' )
PhotoFinish.SavePhoto( fileName, bib, t, frame )
self.queue.task_done()
elif message[0] == 'Terminate':
self.queue.task_done()
self.reset()
break
def stop( self ):
self.queue.put( ['Terminate'] )
self.join()
def save( self, fileName, bib, t, frame ):
self.queue.put( ['Save', fileName, bib, t, frame] )
示例8: Executor
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class Executor(object):
_INTERRUPT = object()
def __init__(self, num_workers=1):
super(Executor, self).__init__()
self._queue = Queue()
self._workers = []
for _ in range(num_workers):
th = Thread(target=self._work)
th.start()
self._workers.append(th)
def submit(self, task):
self._queue.put(task)
def shutdown(self):
for _ in self._workers:
self._queue.put(self._INTERRUPT)
def join(self):
for worker in self._workers:
worker.join()
def _work(self):
while True:
task = self._queue.get(block=True)
if task is self._INTERRUPT:
break
try:
task()
except BaseException as e:
logger.exception(e)
示例9: Search
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class Search(TracePosterior):
"""
Trace and Poutine-based implementation of systematic search.
:param callable model: Probabilistic model defined as a function.
:param int max_tries: The maximum number of times to try completing a trace from the queue.
"""
def __init__(self, model, max_tries=1e6):
"""
Constructor. Default `max_tries` to something sensible - 1e6.
:param callable model: Probabilistic model defined as a function.
:param int max_tries: The maximum number of times to try completing a trace from the queue.
"""
self.model = model
self.max_tries = int(max_tries)
def _traces(self, *args, **kwargs):
"""
algorithm entered here
Running until the queue is empty and collecting the marginal histogram
is performing exact inference
:returns: Iterator of traces from the posterior.
:rtype: Generator[:class:`pyro.Trace`]
"""
# currently only using the standard library queue
self.queue = Queue()
self.queue.put(poutine.Trace())
p = poutine.trace(
poutine.queue(self.model, queue=self.queue, max_tries=self.max_tries))
while not self.queue.empty():
tr = p.get_trace(*args, **kwargs)
yield (tr, tr.log_pdf())
示例10: Pool
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class Pool(object):
class Error(Exception):
pass
def __init__(self, threads, host, port, ssl, user, password):
self._threads=[]
self._queue = Queue(maxsize=1000)
count=0
while len(self._threads) < threads and count < 3* threads:
try:
count+=1
w=Downloader(self._queue, host, port, ssl, user, password)
w.start()
self._threads.append(w)
except SOFT_ERRORS as e:
log.warn('Cannot create downloder thread: %s', e)
if len(self._threads) != threads:
log.error('Cannot create enough workers')
raise Pool.Error('Cannot create enough workers')
def wait_finish(self):
self._queue.join()
def stop(self):
for t in self._threads:
t.stop()
def download(self, **kwargs):
kwargs['retry']=0
self._queue.put(kwargs)
示例11: StoppableThread
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class StoppableThread(threading.Thread):
"""This is thread can be stopped.
Note: Thread by default does not return function result in any case,
which is why I've implemented this workaroung with built-in Queue.
"""
def __init__(self, **kwargs):
super(StoppableThread, self).__init__(**kwargs)
self.__target = kwargs.get('target')
self.__args = kwargs.get('args')
if self.__args is None:
self.__args = ()
self.__kwargs = kwargs.get('kwargs')
if self.__kwargs is None:
self.__kwargs = {}
self.__result_queue = Queue()
self.__stopped = threading.Event()
def stop(self):
"""Stop the thread. It will not terminate code, but set the flag that
should be handled in executed function.
"""
self.__stopped.set()
def is_stopped(self):
"""Check the status of the thread. It only monitors the flag state. If
task is stopped you have to pay attention to `.is_alive()`.
"""
return self.__stopped.is_set()
def run(self):
"""Run the target function, check expected result and propagate
exceptions.
"""
try:
self.__kwargs['_is_stopped'] = self.__stopped.is_set
try:
if self.__target:
func_result = self.__target(*self.__args, **self.__kwargs)
finally:
# Avoid a refcycle if the thread is running a function with
# an argument that has a member that points to the thread.
del self.__target, self.__args, self.__kwargs
if func_result is None:
func_result = {}
elif not isinstance(func_result, dict):
raise TypeError("Task has to return a dict or None.")
except Exception: # pylint: disable=W0703
self.__result_queue.put(traceback.format_exc())
else:
self.__result_queue.put(func_result)
def get_result(self):
"""Return results of target function execution.
"""
self.join()
try:
return self.__result_queue.get_nowait()
except Queue.Empty:
return None
示例12: TagGroup
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
class TagGroup( object ):
'''
Process groups of tag reads and return the best time estimated using quadratic regression.
Stray reads are also detected if there is no quiet period for the tag.
The first read time of each stray read is returned.
'''
def __init__( self ):
self.q = Queue()
self.tagInfo = {}
def add( self, antenna, tag, t, db ):
self.q.put((antenna, tag, t, db))
def flush( self ):
# Process all waiting reads.
while 1:
try:
antenna, tag, t, db = self.q.get(False)
except Empty:
break
try:
self.tagInfo[tag].add( antenna, t, db )
except KeyError:
self.tagInfo[tag] = TagGroupEntry( antenna, t, db )
self.q.task_done()
def getReadsStrays( self, tNow=None, method=QuadraticRegressionMethod, antennaChoice=MostReadsChoice, removeOutliers=True ):
'''
Returns two lists:
reads = [(tag1, t1, sampleSize1, antennaID1), (tag2, t2, sampleSize2, , antennaID2), ...]
strays = [(tagA, tFirstReadA), (tagB, tFirstReadB), ...]
Each stray will be reported as a read the first time it is detected.
'''
self.flush()
trNow = datetimeToTr( tNow or datetime.now() )
reads, strays = [], []
toDelete = []
for tag, tge in six.iteritems(self.tagInfo):
if trNow - tge.lastReadMax >= tQuiet: # Tag has left read range.
if not tge.isStray:
t, sampleSize, antennaID = tge.getBestEstimate(method, antennaChoice, removeOutliers)
reads.append( (tag, t, sampleSize, antennaID) )
toDelete.append( tag )
elif tge.lastReadMax - tge.firstReadMin >= tStray: # This is a stray.
t = trToDatetime( tge.firstReadMin )
if not tge.isStray:
tge.setStray()
reads.append( (tag, t, 1, 0) ) # Report stray first read time.
strays.append( (tag, t) )
for tag in toDelete:
del self.tagInfo[tag]
reads.sort( key=operator.itemgetter(1,0))
strays.sort( key=operator.itemgetter(1,0) )
return reads, strays
示例13: test_item_is_stop_with_cascade_stop
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def test_item_is_stop_with_cascade_stop(self):
"""Return the name of the container that caused the cascade_stop"""
queue = Queue()
for item in QueueItem.stop('foobar-1'), QueueItem.new('a'), QueueItem.new('b'):
queue.put(item)
generator = consume_queue(queue, True)
assert next(generator) is 'foobar-1'
示例14: test_item_is_stop_without_cascade_stop
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def test_item_is_stop_without_cascade_stop(self):
queue = Queue()
for item in QueueItem.stop(), QueueItem.new('a'), QueueItem.new('b'):
queue.put(item)
generator = consume_queue(queue, False)
assert next(generator) == 'a'
assert next(generator) == 'b'
示例15: daily_metadata
# 需要导入模块: from six.moves.queue import Queue [as 别名]
# 或者: from six.moves.queue.Queue import put [as 别名]
def daily_metadata(year, month, day, dst_folder, writers=[file_writer], geometry_check=None,
num_worker_threads=1):
""" Extra metadata for all products in a specific date """
threaded = False
counter = {
'products': 0,
'saved_tiles': 0,
'skipped_tiles': 0,
'skipped_tiles_paths': []
}
if num_worker_threads > 1:
threaded = True
queue = Queue()
# create folders
year_dir = os.path.join(dst_folder, str(year))
month_dir = os.path.join(year_dir, str(month))
day_dir = os.path.join(month_dir, str(day))
product_list = get_products_metadata_path(year, month, day)
logger.info('There are %s products in %s-%s-%s' % (len(list(iterkeys(product_list))),
year, month, day))
for name, product in iteritems(product_list):
product_dir = os.path.join(day_dir, name)
if threaded:
queue.put([product, product_dir, counter, writers, geometry_check])
else:
counter = product_metadata(product, product_dir, counter, writers, geometry_check)
if threaded:
def worker():
while not queue.empty():
args = queue.get()
try:
product_metadata(*args)
except Exception:
exc = sys.exc_info()
logger.error('%s tile skipped due to error: %s' % (threading.current_thread().name,
exc[1].__str__()))
args[2]['skipped_tiles'] += 1
queue.task_done()
threads = []
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
t.start()
threads.append(t)
queue.join()
return counter