Вот тестовый пример:
items = [1,2,3,4,5,6,7,8,9]
count = 0
while items:
print(items.pop(0))
count += 1
if count == 3:
break
print("new while")
while items:
print(items.pop(0))
count += 1
if count == 6:
break
print("last while")
while items:
print(items.pop(0))
count += 1
Благодаря GIL в Python, многие элементы, такие как списки, могут работать одновременно, без их разделения:
import threading, time
items = [1,2,3,4,5,6,7,8,9]
def stuff(x, count_limit):
t = threading.current_thread()
count = 0
while items:
print(t.name + ": " + str(items.pop(0)) + " count: " + str(count) + "\n", end = "")
count += 1
if count_limit:
if count == count_limit:
break
threads = []
counts = [3,6,False]
for x, count in enumerate(counts):
t = threading.Thread(target = stuff, args = (items, count,))
t.name = "Thread " + str(x)
threads.append(t)
для потокав темах: thread.start ()
для тем в темах: thread.join ()