Skip to content

Commit 761d06f

Browse files
committed
added py files
1 parent 34793c0 commit 761d06f

File tree

6 files changed

+180
-2
lines changed

6 files changed

+180
-2
lines changed

source/modules/lesson07/ThreadingMultiprocessing_part3.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ jumbled
1414

1515
2. Try adding a Semaphore to allow 2 threads access at once.
1616

17-
.. TODO::
18-
Review lock_exercise.zip
17+
1918

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def f(x):
2+
return x**2
3+
4+
5+
def integrate(f, a, b, N):
6+
s = 0
7+
dx = (b - a) / N
8+
for i in range(N):
9+
s += f(a + i * dx)
10+
return s * dx
11+
12+
13+
def integrate_f_with_functional_tools(f, a, b, N):
14+
dx = (b - a) / N
15+
return sum(map(f, ((a + y * dx) for y in range(N)))) * dx
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
# imported here so the rest of the code can run without it
29+
import numpy as np
30+
31+
def integrate_numpy(f, a, b, N):
32+
"""
33+
numpy can be used to "vectorize" the problem
34+
35+
f must be "numpy comaptible"
36+
37+
"""
38+
dx = (b - a) / N
39+
i = np.arange(N)
40+
s = np.sum(f(a + (i * dx)))
41+
return s * dx
42+
43+
44+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
3+
import threading
4+
import queue
5+
6+
# from integrate.integrate import integrate, f
7+
from integrate import f, integrate_numpy as integrate
8+
from decorators import timer
9+
10+
11+
@timer
12+
def threading_integrate(f, a, b, N, thread_count=2):
13+
"""break work into N chunks"""
14+
N_chunk = int(float(N) / thread_count)
15+
dx = float(b - a) / thread_count
16+
17+
results = queue.Queue()
18+
19+
def worker(*args):
20+
results.put(integrate(*args))
21+
22+
for i in range(thread_count):
23+
x0 = dx * i
24+
x1 = x0 + dx
25+
thread = threading.Thread(target=worker, args=(f, x0, x1, N_chunk))
26+
thread.start()
27+
print("Thread %s started" % thread.name)
28+
29+
return sum((results.get() for i in range(thread_count)))
30+
31+
32+
if __name__ == "__main__":
33+
34+
# parameters of the integration
35+
a = 0.0
36+
b = 10.0
37+
N = 10**8
38+
thread_count = 1
39+
40+
print("Numerical solution with N=%(N)d : %(x)f" %
41+
{'N': N, 'x': threading_integrate(f, a, b, N, thread_count=thread_count)})
42+
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
import threading
4+
import time
5+
6+
7+
# create a mutable object that is shared among threads
8+
class shared:
9+
val = 1
10+
11+
12+
def func():
13+
y = shared.val
14+
time.sleep(0.00001)
15+
y += 1
16+
shared.val = y
17+
18+
19+
threads = []
20+
# with enough threads, there's sufficient overhead to
21+
# cause a race condition
22+
for i in range(100):
23+
thread = threading.Thread(target=func)
24+
threads.append(thread)
25+
thread.start()
26+
27+
for thread in threads:
28+
thread.join()
29+
30+
print(shared.val)
31+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Simple example of using a threading.Timer
5+
6+
NOTE: The docstring is out of sync with the __init__!
7+
I think it was inherited from the threading.Thread docstring.
8+
"""
9+
10+
import threading
11+
import time
12+
import random
13+
14+
15+
def called_once():
16+
"""
17+
this function is designed to be be called once in the future
18+
"""
19+
print("Hey! I just got called! It's now: {}".format(time.asctime()))
20+
21+
22+
def called_later(count):
23+
"""
24+
This function is designed to run, and then set up a timer to call
25+
itself at a random time in the future
26+
27+
Note that it is limited to 10 invocations
28+
Otherwise, there will always be a background
29+
thread running that can not be easily killed
30+
(at least on *nix -- Windows may let ^C kill it)
31+
32+
Try hitting ^C early in the run...
33+
"""
34+
35+
print("Hey! I just got called for {}th time! It's now: {}"
36+
.format(count, time.asctime()))
37+
# this can trigger another invocation
38+
interval = random.randint(1, 3)
39+
count += 1
40+
if count < 10:
41+
threading.Timer(interval=interval,
42+
function=called_later,
43+
args=(count,)).start()
44+
45+
46+
if __name__ == "__main__":
47+
# use the timer...
48+
49+
# threading.Timer(interval=3, function=called_once).start()
50+
# print("After starting the timer")
51+
# print("it's now: {}".format(time.asctime()))
52+
53+
called_later(0)
54+
55+
# do some stuff:
56+
for i in range(100):
57+
print("{}: nothing important...".format(i))
58+
time.sleep(0.5)
59+
60+
61+
62+

0 commit comments

Comments
 (0)