123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import threading
- import time
- event = threading.Event()
- def test():
- while True:
- event.wait()
- print("test")
- time.sleep(2)
- def start_event_thread_1():
- event.clear()
- threading.Thread(target=test, daemon=True).start()
- time.sleep(3)
- event.set()
- while True:
- pass
- ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- def image_handler():
- thr_num = threading.current_thread().name
- print(f"Идет подготовка изображения из потока [{thr_num}]")
- event.wait()
- print("Изображение оправлено")
- def start_event_thread_2():
- for i in range(10):
- threading.Thread(target=image_handler, name=str(i)).start()
- print(f'Поток [{i}] запущен!')
- time.sleep(1)
- if threading.active_count() > 10:
- event.set()
- ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- cond = threading.Condition()
- def f1():
- while True:
- with cond:
- cond.wait()
- print("Получил событие!")
- def f2():
- for i in range(100):
- if 1 % 10 == 0:
- with cond:
- cond.notify()
- else:
- print(f"f1: {i}")
- time.sleep(1)
- threading.Thread(target=f1).start()
- threading.Thread(target=f2).start()
|