You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.1 KiB
84 lines
2.1 KiB
import threading
|
|
import queue
|
|
import hashlib
|
|
|
|
Qin = queue.Queue()
|
|
Qout = queue.Queue()
|
|
Pool = []
|
|
|
|
def err_msg():
|
|
trace= sys.exc_info()[2]
|
|
try:
|
|
exc_value=str(sys.exc_value)
|
|
except:
|
|
exc_value=''
|
|
return str(traceback.format_tb(trace)),str(sys.exc_type),exc_value
|
|
|
|
def get_errors():
|
|
try:
|
|
while 1:
|
|
yield Qerr.get_nowait()
|
|
except Queue.Empty:
|
|
pass
|
|
|
|
def process_queue():
|
|
flag='ok'
|
|
while flag !='stop':
|
|
try:
|
|
flag,item=Qin.get() #will wait here!
|
|
if flag=='ok':
|
|
m = hashlib.md5()
|
|
m.update(str('iwrupvqb').encode('utf-8') + str(component).encode('utf-8'))
|
|
result = m.hexdigest()
|
|
#print("Thread: " + str() + "Component: " + str(component) + " Result: " + result)
|
|
if(result.startswith('00000')):
|
|
Qout.put({'result': False, 'component': component})
|
|
else:
|
|
Qout.put({'result': False, 'component': component})
|
|
|
|
except:
|
|
Qerr.put(err_msg())
|
|
|
|
def start_threads(amount=5):
|
|
for i in range(amount):
|
|
thread = threading.Thread(target=process_queue)
|
|
thread.start()
|
|
Pool.append(thread)
|
|
def put(data,flag='ok'):
|
|
Qin.put([flag,data])
|
|
|
|
def get(): return Qout.get() #will wait here!
|
|
|
|
def get_all():
|
|
try:
|
|
while 1:
|
|
yield Qout.get_nowait()
|
|
except Queue.Empty:
|
|
pass
|
|
def stop_threads():
|
|
for i in range(len(Pool)):
|
|
Qin.put(('stop',None))
|
|
while Pool:
|
|
time.sleep(1)
|
|
for index,the_thread in enumerate(Pool):
|
|
if the_thread.isAlive():
|
|
continue
|
|
else:
|
|
del Pool[index]
|
|
break
|
|
|
|
component = 9
|
|
for i in (0,1,2,3,4,5,6,7,8,9): put(i)
|
|
|
|
start_threads()
|
|
|
|
found_it = False
|
|
while found_it == False:
|
|
ret = get()
|
|
if ret['result'] == True:
|
|
stop_threads()
|
|
found_it = True
|
|
print("Santa needs the number " + str(ret['component']))
|
|
else:
|
|
component += 1
|
|
put(component)
|
|
|