Skip to content

Instantly share code, notes, and snippets.

@Zheaoli
Created January 11, 2021 13:51
Show Gist options
  • Save Zheaoli/0be03a606f20153f54a8b078d7c96a5e to your computer and use it in GitHub Desktop.
Save Zheaoli/0be03a606f20153f54a8b078d7c96a5e to your computer and use it in GitHub Desktop.
import threading
from concurrent.futures import ThreadPoolExecutor
from typing import List
LOCK = threading.Lock()
def task(flag: int, queue: List[int], count: int, mod: int):
while True:
with LOCK:
if count == len(queue):
return
if not queue:
if flag == 0:
queue.append(0)
print(f"task-{flag} number:{0}")
elif queue and (
(flag == 0 and queue[-1] % mod + 1 == mod)
or (flag != 0 and queue[-1] % mod + 1 == flag)
):
temp_value = queue[-1] + 1
print(f"task-{flag} number:{temp_value}")
queue.append(temp_value)
def main():
task_number = 3
count = 10
queue = list()
pool = ThreadPoolExecutor(max_workers=task_number)
for i in range(task_number):
pool.submit(task, i, queue, count, task_number)
pool.shutdown(wait=True)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment