Skip to content

Instantly share code, notes, and snippets.

@badjano
Created January 9, 2018 19:47
Show Gist options
  • Save badjano/83e2a50440b3229ce931beddf9af437a to your computer and use it in GitHub Desktop.
Save badjano/83e2a50440b3229ce931beddf9af437a to your computer and use it in GitHub Desktop.
Simple example of threading using all cpu threads available
from threading import Thread, Lock
from multiprocessing import cpu_count
from queue import Queue
import math
import random
lock = Lock()
def do_work(name, t):
test = [math.tanh(a - b) for a in range(random.randint(500, 2000)) for b in range(random.randint(500, 2000))]
lock.acquire()
print("test done %.10f in %s on thread %d" % (sum(test), name, t))
lock.release()
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"]
names = ["%s_%d" % (l, i) for l in letters for i in range(10)]
def worker():
item = q.get()
while len(names) > 0:
do_work(names.pop(0), item)
q.task_done()
q = Queue()
for i in range(cpu_count()):
q.put(i)
t = Thread(target=worker)
t.daemon = True
t.start()
q.join() # block until all tasks are done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment