forked from dbradul/python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_threads.py
More file actions
229 lines (208 loc) · 5.87 KB
/
lesson_threads.py
File metadata and controls
229 lines (208 loc) · 5.87 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import time
import threading
import multiprocessing
import itertools
import os
import logging
import random
import string
import requests
from functools import partial
from multiprocessing import Queue
from multiprocessing.pool import ThreadPool
from PIL import Image
from io import BytesIO
from typing import List
class timer():
def __init__(self, message):
self.message = message
def __enter__(self):
self.start = time.time()
return None
def __exit__(self, type, value, traceback):
elapsed_time = (time.time() - self.start)
print(self.message.format(elapsed_time))
#
#
# ### THREADS
# DATA_SIZE = 1
#
# def long_running_task(n=1):
# # print(threading.current_thread())
# time.sleep(n)
#
# with timer('Elapsed: {}s'):
# long_running_task()
# long_running_task()
#
# with timer('Elapsed: {}s'):
# t1 = threading.Thread(target=countdown, args=(DATA_SIZE // 2,))
# t2 = threading.Thread(target=countdown, args=(DATA_SIZE // 2,))
# t1.start()
# t2.start()
# t1.join()
# t2.join()
#
#
# def run_threads(data, workers):
# threads = [
# threading.Thread(target=countdown, args=(data // workers, ))
# for _ in range(workers)
# ]
# for t in threads:
# t.start()
# for t in threads:
# t.join()
#
# workers = 20
# with timer('Elapsed: {}s'):
# run_threads(DATA_SIZE, workers)
#
# with timer('Elapsed: {}s'):
# with ThreadPool(workers) as pool:
# input_data = [DATA_SIZE // workers for _ in range(workers)]
# pool.map(long_running_task, input_data)
#
#
# ### IO-BOUND
#
# def fetch_pic(num_pic):
# # def fetch_pic(num_pic, path):
# url = 'https://picsum.photos/400/600'
# path = './pics'
# for _ in range(num_pic):
# random_name = ''.join(random.choices(string.ascii_letters + string.digits, k=5))
# response = requests.get(url)
# if response.status_code == 200:
# with open(f'{path}/{random_name}.jpg', 'wb') as f:
# f.write(response.content)
# print(f"Fetched pic [{os.getpid()}]: {f.name}")
# else:
# print(f"Error {response.status_code}")
#
# with timer('Elapsed: {}s'):
# fetch_pic(100)
#
#
# DATA_SIZE = 100
# workers = 10
#
# with timer('Elapsed: {}s'):
# with ThreadPool(workers) as pool:
# input_data = [DATA_SIZE // workers for _ in range(workers)]
# pool.map(fetch_pic, input_data)
#
#
# ### CPU-BOUND
#
#
# DATA_SIZE = 10000000
#
# def fill_data(n, lst):
# # print(threading.current_thread())
# while n > 0:
# n -= 1
# lst.append(random.randint(1, 100))
#
#
# lst = []
# with timer('Elapsed: {}s'):
# # fill_data(DATA_SIZE, lst)
# fill_data(DATA_SIZE, lst)
#
#
# with timer('Elapsed: {}s'):
# t1 = threading.Thread(target=fill_data, args=(DATA_SIZE // 2, lst))
# t2 = threading.Thread(target=fill_data, args=(DATA_SIZE // 2, lst))
# t1.start()
# t2.start()
# t1.join()
# t2.join()
#
# print(os.cpu_count())
#
# workers = 4
# with timer('Elapsed: {}s'):
# with ThreadPool(workers) as pool:
# input_data = [DATA_SIZE // workers for _ in range(workers)]
# pool.map(partial(fill_data, lst=lst), input_data)
#
# workers = 8
# with timer('Elapsed: {}s'):
# with multiprocessing.Pool(workers) as pool:
# input_data = [DATA_SIZE // workers for _ in range(workers)]
# pool.map(partial(fill_data, lst=lst), input_data)
image = Image.open('pics/0Ay6r.jpg').convert('L')
image.save('pics/0Ay6r_greyscale.jpg')
def factorize_naive(n):
""" A naive factorization method. Take integer 'n', return list of
factors.
"""
if n < 2:
return []
factors = []
p = 2
while True:
if n == 1:
return factors
r = n % p
if r == 0:
factors.append(p)
n = n / p
elif p * p >= n:
factors.append(n)
return factors
elif p > 2:
# Advance in steps of 2 over odd numbers
p += 2
else:
# If p == 2, get to 3
p += 1
assert False, "unreachable"
# single-threaded version
# result = {}
result2 = []
data = [n for n in range(1000000)]
DATA_SIZE = 1000
workers = 8
queue = Queue(maxsize=1000)
# with timer('Elapsed: {}s'):
# for n in data:
# result[n] = factorize_naive(n)
# def factorize_numbers(lst):
# for n in lst:
# result[n] = factorize_naive(n)
def factorize_number(n):
# result[n] = factorize_naive(n)
return n, factorize_naive(n)
# queue.put((n, factorize_naive(n)))
with timer('Elapsed: {}s'):
# with multiprocessing.Pool(workers) as pool:
with ThreadPool(workers) as pool:
input_data = [n for n in range(DATA_SIZE)] #[DATA_SIZE // workers for _ in range(workers)]
result = pool.map(factorize_number, input_data)
# def factorize_number2(queue, n):
# # result[n] = factorize_naive(n)
# queue.put((n, factorize_naive(n)))
#
# with timer('Elapsed: {}s'):
# with multiprocessing.Pool(workers) as pool:
# # with ThreadPool(workers) as pool:
# input_data = [n for n in range(DATA_SIZE)] #[DATA_SIZE // workers for _ in range(workers)]
# pool.map(partial(factorize_number2, queue=queue), input_data)
# def factorize_number(input, result):
# chunk_number, data_size = input
# chunk_offset = chunk_number * (DATA_SIZE // workers)
# for n in range(data_size):
# result[chunk_offset + n] = factorize_naive(chunk_offset + n)
#
#
# with timer('Elapsed: {}s'):
# with multiprocessing.Pool(workers) as pool:
# # with ThreadPool(workers) as pool:
# input_data = [(i, DATA_SIZE // workers) for i in range(workers)] #[DATA_SIZE // workers for _ in range(workers)]
# pool.map(partial(factorize_number, result=result), input_data)
# while not queue.empty():
# n, factors = queue.get()
# result[n] = factors
print(result)