本文整理匯總了Python中multiprocessing.queues.SimpleQueue.get方法的典型用法代碼示例。如果您正苦於以下問題:Python SimpleQueue.get方法的具體用法?Python SimpleQueue.get怎麽用?Python SimpleQueue.get使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiprocessing.queues.SimpleQueue
的用法示例。
在下文中一共展示了SimpleQueue.get方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Logger
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class Logger(object):
def __init__(self, logfilepath):
try:
os.remove(logfilepath)
except OSError:
pass
self.logfilepath = logfilepath
self.logq = SimpleQueue()
self.tags = ''
self.num_tags = 0
def add_tag(self, tag):
#self.log("adding tag {}".format(tag))
self.num_tags += 1
if self.tags != '':
self.tags = self.tags + '.' + tag
else:
self.tags = tag
def remove_tag(self):
#self.log("removing tag")
tags = self.tags.split('.')
self.tags = ".".join(tags[:-1])
self.num_tags -= 1
def get_tag_part(self):
if self.tags != '':
return self.tags + ": "
else:
return ''
def log(self, message, start_group=None, end_group=None):
assert(type(message)==str)
self.logq.put(" "*self.num_tags*4 + self.get_tag_part() + message + '\n')
def getlog(self):
return self.logq.get()
def getlogs(self, n=None):
logs = []
if n == None:
while not self.logq.empty():
logs.append(self.getlog())
else:
assert(type(n)==int)
while not (self.logq.empty() or len(logs) == n):
logs.append(self.getlog())
return logs
def write_to_file(self):
# mode 'a' for append
with open(self.logfilepath, 'a') as f:
f.writelines(self.getlogs())
示例2: test_can_pickle_via_queue
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def test_can_pickle_via_queue(self):
"""
https://github.com/andresriancho/w3af/issues/8748
"""
sq = SimpleQueue()
u1 = URL('http://www.w3af.com/')
sq.put(u1)
u2 = sq.get()
self.assertEqual(u1, u2)
示例3: __init__
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class TableInterface:
def __init__(self):
self.input_interface = Queue()
self.output_interface = None
def put(self, data):
self.output_interface.put(data)
def get(self):
return self.input_interface.get()
示例4: main
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def main():
sfile = settings.BIG_FILE
fsize = os.path.getsize(sfile)
with open(sfile, "r") as fh:
chunks = size_chunks(fh, fsize, num_chunks=settings.BIGFILE_MP_CHUNKS)
# Debug
# for c in chunks:
# print(c)
q = Queue()
pattern = re.compile(settings.TARGET_USERNAME)
# consumer
# con = multiprocessing.Process(target=opener, args=(cat(grep(pattern, writer())),))
# con.daemon = True
# con.start()
# producer
producers = []
file_handles = []
for chunk in chunks:
fh = open(sfile, "r")
file_handles.append(fh)
o = opener(cat(chunk, grep(pattern, writer(q))))
t = multiprocessing.Process(target=sender, args=(o,))
t.daemon = True
producers.append(t)
for p in producers:
p.start()
for p in producers:
p.join()
# con.join()
q.put(None) # sentinel
for f in file_handles:
f.close()
recsmatch = 0
print("Before queue comp")
while True:
x = q.get()
if x == None:
break
recsmatch += 1
print("After queue comp")
print("recsmatch={r} chunks={c}".format(r=recsmatch, c=settings.BIGFILE_MP_CHUNKS))
示例5: StatusTracker
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class StatusTracker(object):
def __init__(self):
self.logq = SimpleQueue()
self.history = []
def put(self, msg):
assert(type(msg)==str)
self.logq.put(msg)
def flushq(self):
while not self.logq.empty():
self.history.append(self.logq.get())
self.prune_history()
def prune_history(self):
self.history = self.history[-100:]
示例6: launch_graph_plot
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def launch_graph_plot():
q = SimpleQueue()
Pyro4.config.HOST="10.1.1.2"
daemon = Pyro4.Daemon()
ns = Pyro4.locateNS()
p = Process(target=_launch_daemon, args=(daemon, q,))
p.start()
graph_plot = GraphPlotPanel()
while True:
if not q.empty():
item = q.get()
if item[0] == 'time':
print "got queue:", item
graph_plot.set_time(item[1])
elif item[0] == 'vertex_color':
pass
graph_plot.run()
fpsClock.tick(60)
示例7: RangerControlServer
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class RangerControlServer(HTTPServer):
def __init__(self, fm):
self.fm = fm
self.queue = SimpleQueue()
self.goDie = False
HTTPServer.__init__(self, ("127.0.0.1", 5964), RangerControlHandler)
def start(self):
self.thread = threading.Thread(target=self.process)
self.thread.start()
def stop(self):
self.shutdown()
def process(self):
self.serve_forever()
def check_messages(self):
if self.queue.empty():
return None
return self.queue.get()
def act_on_messages(self):
msg = self.check_messages()
if msg == None:
return False
action, arg = msg
match = re.match(r"/cdtab-(\S+)", action)
if match != None:
tab = match.group(1)
if not (tab in self.fm.tabs):
self.fm.tab_open(tab, arg)
else:
self.fm.tabs[tab].enter_dir(arg)
elif action == "/cd":
self.fm.enter_dir(arg)
elif action == "/cdfirst":
first_tab = self.fm._get_tab_list()[0]
self.fm.tabs[first_tab].enter_dir(arg)
else:
self.fm.notify("Unknown server command", bad=True)
return True
示例8: __init__
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class ErrorMonitor:
def __init__(self):
self.pipe = SimpleQueue()
self.message = None
def main(self):
while True:
message = self.pipe.get()
if message != 'Q':
self.message = message[1:]
LongJump.longjump()
break
else:
self.pipe = None
break
def haserror(self):
""" master only """
return self.message is not None
def start(self):
""" master only """
self.thread = Thread(target=self.main)
self.thread.daemon = True
self.thread.start()
def join(self):
""" master only """
try:
self.pipe.put('Q')
self.thread.join()
except:
pass
finally:
self.thread = None
def slaveraise(self, type, error, traceback):
""" slave only """
message = 'E' * 1 + pickle.dumps((type,
''.join(tb.format_exception(type, error, traceback))))
if self.pipe is not None:
self.pipe.put(message)
示例9: _handle_ASes
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def _handle_ASes(self):
"""Spawns several processes (based on the available CPUs) to handle the
AS resolving and creates the necessary objects based on the results.
"""
# Gather all the ASNs seen through filter and recursive resolving.
all_ASNs = list((self.recursed_ASes | self.AS_list) - self.black_list)
all_ASNs_count = len(all_ASNs)
if all_ASNs_count < 1:
return
# We will devote all but one core to resolving since the main process
# will handle the objects' creation.
number_of_resolvers = mp.cpu_count() - 1
if number_of_resolvers < 1:
number_of_resolvers = 1
# The list of ASNs is going to be distributed almost equally to the
# available resolvers.
if all_ASNs_count >= number_of_resolvers:
slice_length = int(math.ceil(all_ASNs_count / float(number_of_resolvers)))
else:
number_of_resolvers = all_ASNs_count
slice_length = 1
result_q = SimpleQueue() # NOTE: Only works with this queue.
processes = []
slice_start = 0
for i in xrange(number_of_resolvers):
ASN_batch = all_ASNs[slice_start:slice_start+slice_length]
processes.append(mp.Process(target=_subprocess_AS_resolving, args=(ASN_batch, result_q)).start())
slice_start += slice_length
# PROGRESS START
# Show progress while running.
# Can be safely commented out until PROGRESS END.
aps_count = 0
aps = 0
time_start = time.time()
# PROGRESS END
done = 0
while done < all_ASNs_count:
try:
asn, routes = result_q.get()
except Empty:
# This should never be reached with this queue but left here
# just in case.
time.sleep(0.2)
continue
# If the AS has routes create the appropriate ASN object and add it
# to the data pool.
if routes is not None and (routes['ipv4'] or routes['ipv6']):
ASN_object = rpsl.ASObject(asn)
for prefix in routes['ipv4']:
route_object = rpsl.RouteObject(prefix, asn)
ASN_object.route_obj_dir.append_route_obj(route_object)
for prefix in routes['ipv6']:
route6_object = rpsl.Route6Object(prefix, asn)
ASN_object.route_obj_dir.append_route_obj(route6_object)
self.AS_dir.append_ASN_obj(ASN_object)
done += 1
# PROGRESS START
# Show progress while running.
# Can be safely commented out until PROGRESS END.
aps_count += 1
time_diff = time.time() - time_start
if time_diff >= 1:
aps = aps_count / time_diff
aps_count = 0
time_start = time.time()
sys.stdout.write("{} of {} ASes | {:.0f} ASes/s \r"
.format(done, all_ASNs_count, aps))
sys.stdout.flush()
print
示例10: spawn_import_clients
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def spawn_import_clients(options, files_info):
# Spawn one reader process for each db.table, as well as many client processes
task_queue = SimpleQueue()
error_queue = SimpleQueue()
exit_event = multiprocessing.Event()
interrupt_event = multiprocessing.Event()
errors = []
reader_procs = []
client_procs = []
parent_pid = os.getpid()
signal.signal(signal.SIGINT, lambda a, b: abort_import(a, b, parent_pid, exit_event, task_queue, client_procs, interrupt_event))
try:
progress_info = []
rows_written = multiprocessing.Value(ctypes.c_longlong, 0)
for i in xrange(options["clients"]):
client_procs.append(multiprocessing.Process(target=client_process,
args=(options["host"],
options["port"],
options["auth_key"],
task_queue,
error_queue,
rows_written,
options["force"],
options["durability"])))
client_procs[-1].start()
for file_info in files_info:
progress_info.append((multiprocessing.Value(ctypes.c_longlong, -1), # Current lines/bytes processed
multiprocessing.Value(ctypes.c_longlong, 0))) # Total lines/bytes to process
reader_procs.append(multiprocessing.Process(target=table_reader,
args=(options,
file_info,
task_queue,
error_queue,
progress_info[-1],
exit_event)))
reader_procs[-1].start()
# Wait for all reader processes to finish - hooray, polling
while len(reader_procs) > 0:
time.sleep(0.1)
# If an error has occurred, exit out early
if not error_queue.empty():
exit_event.set()
reader_procs = [proc for proc in reader_procs if proc.is_alive()]
update_progress(progress_info)
# Wait for all clients to finish
alive_clients = sum([client.is_alive() for client in client_procs])
for i in xrange(alive_clients):
task_queue.put("exit")
while len(client_procs) > 0:
time.sleep(0.1)
client_procs = [client for client in client_procs if client.is_alive()]
# If we were successful, make sure 100% progress is reported
if error_queue.empty() and not interrupt_event.is_set():
print_progress(1.0)
def plural(num, text):
return "%d %s%s" % (num, text, "" if num == 1 else "s")
# Continue past the progress output line
print("")
print("%s imported in %s" % (plural(rows_written.value, "row"),
plural(len(files_info), "table")))
finally:
signal.signal(signal.SIGINT, signal.SIG_DFL)
if interrupt_event.is_set():
raise RuntimeError("Interrupted")
if not task_queue.empty():
error_queue.put((RuntimeError, RuntimeError("Error: Items remaining in the task queue"), None))
if not error_queue.empty():
# multiprocessing queues don't handling tracebacks, so they've already been stringified in the queue
while not error_queue.empty():
error = error_queue.get()
print("%s" % error[1], file=sys.stderr)
if options["debug"]:
print("%s traceback: %s" % (error[0].__name__, error[2]), file=sys.stderr)
if len(error) == 4:
print("In file: %s" % error[3], file=sys.stderr)
raise RuntimeError("Errors occurred during import")
示例11: magic_memit
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def magic_memit(self, line=''):
"""Measure memory usage of a Python statement
Usage, in line mode:
%memit [-ir<R>t<T>] statement
Options:
-r<R>: repeat the loop iteration <R> times and take the best result.
Default: 1
-i: run the code in the current environment, without forking a new process.
This is required on some MacOS versions of Accelerate if your line contains
a call to `np.dot`.
-t<T>: timeout after <T> seconds. Unused if `-i` is active. Default: None
Examples
--------
::
In [1]: import numpy as np
In [2]: %memit np.zeros(1e7)
maximum of 1: 76.402344 MB per loop
In [3]: %memit np.ones(1e6)
maximum of 1: 7.820312 MB per loop
In [4]: %memit -r 10 np.empty(1e8)
maximum of 10: 0.101562 MB per loop
In [5]: memit -t 3 while True: pass;
Subprocess timed out.
Subprocess timed out.
Subprocess timed out.
ERROR: all subprocesses exited unsuccessfully. Try again with the `-i`
option.
maximum of 1: -inf MB per loop
"""
opts, stmt = self.parse_options(line, 'r:t:i', posix=False, strict=False)
repeat = int(getattr(opts, 'r', 1))
if repeat < 1:
repeat == 1
timeout = int(getattr(opts, 't', 0))
if timeout <= 0:
timeout = None
run_in_place = hasattr(opts, 'i')
# Don't depend on multiprocessing:
try:
import multiprocessing as pr
from multiprocessing.queues import SimpleQueue
q = SimpleQueue()
except ImportError:
class ListWithPut(list):
"Just a list where the `append` method is aliased to `put`."
def put(self, x):
self.append(x)
q = ListWithPut()
print ('WARNING: cannot import module `multiprocessing`. Forcing the'
'`-i` option.')
run_in_place = True
ns = self.shell.user_ns
if run_in_place:
for _ in xrange(repeat):
_get_usage(q, stmt, ns=ns)
else:
# run in consecutive subprocesses
at_least_one_worked = False
for _ in xrange(repeat):
p = pr.Process(target=_get_usage, args=(q, stmt, 'pass', ns))
p.start()
p.join(timeout=timeout)
if p.exitcode == 0:
at_least_one_worked = True
else:
p.terminate()
if p.exitcode == None:
print('Subprocess timed out.')
else:
print('Subprocess exited with code %d.' % p.exitcode)
q.put(float('-inf'))
if not at_least_one_worked:
print ('ERROR: all subprocesses exited unsuccessfully. Try again '
'with the `-i` option.')
usages = [q.get() for _ in xrange(repeat)]
usage = max(usages)
print('maximum of %d: %f MB per loop' % (repeat, usage))
示例12: magic_memit
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def magic_memit(ns, line='', repeat=1, timeout=None, run_in_place=True):
"""Measure memory usage of a Python statement
Usage, in line mode:
%memit [-ir<R>t<T>] statement
Options:
-r<R>: repeat the loop iteration <R> times and take the best result.
Default: 3
-i: run the code in the current environment, without forking a new process.
This is required on some MacOS versions of Accelerate if your line contains
a call to `np.dot`.
-t<T>: timeout after <T> seconds. Unused if `-i` is active. Default: None
Examples
--------
::
In [1]: import numpy as np
In [2]: %memit np.zeros(1e7)
maximum of 3: 76.402344 MB per loop
In [3]: %memit np.ones(1e6)
maximum of 3: 7.820312 MB per loop
In [4]: %memit -r 10 np.empty(1e8)
maximum of 10: 0.101562 MB per loop
In [5]: memit -t 3 while True: pass;
Subprocess timed out.
Subprocess timed out.
Subprocess timed out.
ERROR: all subprocesses exited unsuccessfully. Try again with the `-i`
option.
maximum of 3: -inf MB per loop
"""
if repeat < 1:
repeat == 1
if timeout <= 0:
timeout = None
# Don't depend on multiprocessing:
try:
import multiprocessing as pr
from multiprocessing.queues import SimpleQueue
q = SimpleQueue()
except ImportError:
class ListWithPut(list):
"Just a list where the `append` method is aliased to `put`."
def put(self, x):
self.append(x)
q = ListWithPut()
print ('WARNING: cannot import module `multiprocessing`. Forcing the'
'`-i` option.')
run_in_place = True
def _get_usage(q, stmt, setup='pass', ns={}):
from memory_profiler import memory_usage as _mu
try:
exec setup in ns
_mu0 = _mu()[0]
exec stmt in ns
_mu1 = _mu()[0]
q.put(_mu1 - _mu0)
except Exception as e:
q.put(float('-inf'))
raise e
if run_in_place:
for _ in xrange(repeat):
_get_usage(q, line, ns=ns)
else:
# run in consecutive subprocesses
at_least_one_worked = False
for _ in xrange(repeat):
p = pr.Process(target=_get_usage, args=(q, line, 'pass', ns))
p.start()
p.join(timeout=timeout)
if p.exitcode == 0:
at_least_one_worked = True
else:
p.terminate()
if p.exitcode == None:
print 'Subprocess timed out.'
else:
print 'Subprocess exited with code %d.' % p.exitcode
q.put(float('-inf'))
if not at_least_one_worked:
raise RuntimeError('ERROR: all subprocesses exited unsuccessfully.'
' Try again with the `-i` option.')
usages = [q.get() for _ in xrange(repeat)]
usage = max(usages)
return usage
示例13: run_clients
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
def run_clients(options, db_table_set):
# Spawn one client for each db.table
exit_event = multiprocessing.Event()
processes = []
error_queue = SimpleQueue()
interrupt_event = multiprocessing.Event()
stream_semaphore = multiprocessing.BoundedSemaphore(options["clients"])
signal.signal(signal.SIGINT, lambda a, b: abort_export(a, b, exit_event, interrupt_event))
try:
progress_info = []
for db, table in db_table_set:
progress_info.append((multiprocessing.Value(ctypes.c_longlong, -1),
multiprocessing.Value(ctypes.c_longlong, 0)))
processes.append(multiprocessing.Process(target=export_table,
args=(options["host"],
options["port"],
options["auth_key"],
db, table,
options["directory_partial"],
options["fields"],
options["format"],
error_queue,
progress_info[-1],
stream_semaphore,
exit_event)))
processes[-1].start()
# Wait for all tables to finish
while len(processes) > 0:
time.sleep(0.1)
if not error_queue.empty():
exit_event.set() # Stop rather immediately if an error occurs
processes = [process for process in processes if process.is_alive()]
update_progress(progress_info)
# If we were successful, make sure 100% progress is reported
# (rows could have been deleted which would result in being done at less than 100%)
if error_queue.empty() and not interrupt_event.is_set():
print_progress(1.0)
# Continue past the progress output line and print total rows processed
def plural(num, text):
return "%d %s%s" % (num, text, "" if num == 1 else "s")
print("")
print("%s exported from %s" % (plural(sum([info[0].value for info in progress_info]), "row"),
plural(len(db_table_set), "table")))
finally:
signal.signal(signal.SIGINT, signal.SIG_DFL)
if interrupt_event.is_set():
raise RuntimeError("Interrupted")
if not error_queue.empty():
# multiprocessing queues don't handling tracebacks, so they've already been stringified in the queue
while not error_queue.empty():
error = error_queue.get()
print("%s" % error[1], file=sys.stderr)
if options["debug"]:
print("%s traceback: %s" % (error[0].__name__, error[2]), file=sys.stderr)
raise RuntimeError("Errors occurred during export")
示例14: PrimitiveProcess
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
#.........這裏部分代碼省略.........
elif isinstance(self._instruction, I.CTR):
self._run = partial(execute_CTR,
reason=self._instruction.reason)
elif isinstance(self._instruction, I.ADD):
self._run = partial(execute_ADD,
field=self._instruction.field,
size=self._instruction.size)
elif isinstance(self._instruction, I.RMV):
self._run = partial(execute_RMV,
field=self._instruction.field)
elif isinstance(self._instruction, I.LD):
self._run = partial(execute_LD,
destination=self._instruction.destination,
source=self._instruction.source)
elif isinstance(self._instruction, I.ST):
self._run = partial(execute_ST,
location=self._instruction.location,
source=self._instruction.source)
elif isinstance(self._instruction, I.OP):
self._run = partial(execute_OP,
destination=self._instruction.destination,
left_source=self._instruction.left_source,
operator=self._instruction.operator,
right_source=self._instruction.right_source)
elif isinstance(self._instruction, I.PUSH):
self._run = partial(execute_PUSH,
location=self._instruction.location)
elif isinstance(self._instruction, I.POP):
self._run = partial(execute_POP,
location=self._instruction.location)
elif isinstance(self._instruction, I.BR):
self._run = partial(execute_BR,
left_source=self._instruction.left_source,
operator=self._instruction.operator,
right_source=self._instruction.right_source,
label=self._instruction.label)
elif isinstance(self._instruction, I.JMP):
self._run = partial(execute_JMP,
label=self._instruction.label)
elif isinstance(self._instruction, I.LBL):
self._run = execute_LBL
elif isinstance(self._instruction, I.LDt):
self.table_interface = TableInterface()
self._run = partial(execute_LDt,
table_interface=self.table_interface,
instruction=self._instruction,
destinations=self._instruction.destinations,
index=self._instruction.index)
elif isinstance(self._instruction, I.STt):
self.table_interface = TableInterface()
self._run = partial(execute_STt,
table_interface=self.table_interface,
instruction=self._instruction,
index=self._instruction.index,
sources=self._instruction.sources)
elif isinstance(self._instruction, I.INCt):
self.table_interface = TableInterface()
self._run = partial(execute_INCt,
table_interface=self.table_interface,
instruction=self._instruction,
index=self._instruction.index)
elif isinstance(self._instruction, I.LKt):
self.table_interface = TableInterface()
self._run = partial(execute_LKt,
table_interface=self.table_interface,
instruction=self._instruction,
index=self._instruction.index,
sources=self._instruction.sources)
elif isinstance(self._instruction, I.CRC):
self._run = partial(execute_CRC,
destination=self._instruction.destination,
sources=self._instruction.sources)
elif isinstance(self._instruction, I.HSH):
self._run = partial(execute_HSH,
destination=self._instruction.destination,
sources=self._instruction.sources)
elif isinstance(self._instruction, I.HLT):
self._run = execute_HLT
else:
raise RuntimeError()
def stop(self):
self.input_interface.put(None)
self.join()
def run(self):
while True:
try:
state = self.input_interface.get()
# print 'primitive_process'
if state is None:
return
state = self._run(state)
self.output_interfaces[state.label].put(state)
except KeyboardInterrupt:
break
示例15: Table
# 需要導入模塊: from multiprocessing.queues import SimpleQueue [as 別名]
# 或者: from multiprocessing.queues.SimpleQueue import get [as 別名]
class Table(Process):
def __init__(self, patterns):
super(Table, self).__init__()
self.patterns = patterns
self.input_interface = Queue()
self.output_interfaces = {'': Queue()}
def stop(self):
self.input_interface.put(None)
self.join()
def run(self):
while True:
try:
data = self.input_interface.get()
if data is None:
return
operation, items = data
if operation == 'add_entry':
index, entry = items
self.patterns.add_entry(index, entry)
elif operation == 'del_entry':
index = items
self.patterns.del_entry(index)
elif operation == 'query_entry':
index = items
entry = self.patterns.query_entry(index)
self.output_interfaces[''].put(entry)
elif operation == 'write':
index, pattern = items
self.patterns[index] = pattern
elif operation == 'read':
index, instruction_id = items
pattern = self.patterns[index]
self.output_interfaces[instruction_id].put(pattern)
elif operation == 'lookup':
values, instruction_id = items
value = -1
if isinstance(self.patterns, MatchPatterns):
for i in range(0, len(self.patterns)):
pattern_list = self.patterns[i].values()
if len(pattern_list) != len(values):
raise RuntimeError()
if all(map((lambda (value, mask), source: value.value == (source.value & mask)),
pattern_list, values)):
value = i
break
elif isinstance(self.patterns, SimplePatterns):
for i in range(0, len(self.patterns)):
pattern_list = self.patterns[i].values()
if all(map((lambda value, source: value.value == source.value),
pattern_list, values)):
value = i
break
else:
raise RuntimeError()
self.output_interfaces[instruction_id].put(value)
else:
raise RuntimeError()
except KeyboardInterrupt:
break