티스토리 뷰

[폰트사이즈18 고딕 진하게]



workqueue 에 push 하고


pop 할때 마다 매번 다른 쓰레드로 동작





import threading
import time


def foo(t):
i = 0
while True:
print(threading.currentThread().getName(), i)
i += 1
time.sleep(t)


class WorkQueue:
def __init__(self):
self.works = list()

def push(self, cb, *args, **kwargs):
self.works.append((cb, args, kwargs))

def pop(self):
if len(self.works):
work, args, kwargs = self.works.pop(0)
t = threading.Thread(target=work, args=args, kwargs=kwargs)
t.start()


works = WorkQueue()
works.push(foo, 0.1)
works.push(foo, 1)
works.push(foo, 3)
works.pop()
works.pop()
works.pop()







댓글