-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_threading.py
73 lines (55 loc) · 1.66 KB
/
try_threading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
A basic example of how to use threading to generate images.
This gives only a small improvement and I'm still trying
to figure out why.
"""
import queue
import threading
from mandelpy import create_image, Settings, power
from PIL import ImageFilter
images_folder = r"..\images\increasing_powers5"
video_file = r"..\test5.mp4"
# The queue for tasks
q = queue.Queue()
# Worker, handles each task
def worker():
while True:
i = q.get()
if i is None:
break
n = 1 + i/50
p = (n - 2) * abs(n - 2) + 2
settings = Settings(transform=lambda z: power(z, p) + 1.5 * z - 0.5 - 0.25j,
width=2000, height=2000,
block_size=(1000, 1000),
mirror_x=False)
img = create_image(settings, verbose=True)
img = img.filter(ImageFilter.GaussianBlur(1))
img = img.resize((1920, 1080))
img.save(rf"{images_folder}\Pic{i}.jpg", optimize=True, quality=90)
q.task_done()
def start_workers(worker_pool=1000):
threads = []
for i in range(worker_pool):
t = threading.Thread(target=worker)
t.start()
threads.append(t)
return threads
def stop_workers(threads):
# stop workers
for i in threads:
q.put(None)
for t in threads:
t.join()
def create_queue(task_items):
for item in task_items:
q.put(item)
if __name__ == "__main__":
# Dummy tasks
tasks = [item for item in range(100)]
# Start up your workers
workers = start_workers(worker_pool=2)
create_queue(tasks)
# Blocks until all tasks are complete
q.join()
stop_workers(workers)