Skip to content

Instantly share code, notes, and snippets.

@morris821028
Created July 4, 2017 03:36
Show Gist options
  • Save morris821028/87031485cfb00372824a374560b15c2c to your computer and use it in GitHub Desktop.
Save morris821028/87031485cfb00372824a374560b15c2c to your computer and use it in GitHub Desktop.
Testing Parallel-judge
#!/usr/bin/python3
import threading
import time
import queue
import random
def work(wid, wlock, wset, q):
while True:
if not q.empty():
config = q.get()
print ("wid %d, solve %d" % (wid, config[0]))
time.sleep(random.randint(0, 4))
wlock.acquire()
wset.remove(config[0])
wlock.release()
time.sleep(1)
workers = []
wqueues = []
wlock = threading.Lock()
wset = set()
for i in range(2):
q = queue.Queue()
worker = threading.Thread(target=work, args=[i, wlock, wset, q])
worker.start()
workers.append(worker)
wqueues.append(q)
while True:
pro = [random.randint(0, 10)]
if pro[0] not in wset:
for i in range(0, len(wqueues)):
if wqueues[i].empty():
wqueues[i].put(pro)
wlock.acquire()
wset.add(pro[0])
wlock.release()
break
else:
print ('duplicate %d' % (pro[0]))
time.sleep(0.5)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment