make example consistent with blog

This commit is contained in:
Maarten A. Breddels 2021-01-06 19:21:57 +01:00
parent 27ebe7646c
commit 9e72e0fb3d
3 changed files with 29 additions and 13 deletions

View File

@ -133,15 +133,15 @@ N = 1024*1024*32
M = 4
x = np.arange(N, dtype='f8')
def run():
def some_numpy_computation():
total = 0
for i in range(M):
total += x.sum()
return total
thread1 = threading.Thread(target=run)
thread2 = threading.Thread(target=run)
thread1 = threading.Thread(target=some_numpy_computation)
thread2 = threading.Thread(target=some_numpy_computation)
def main(args=None):
thread1.start()

View File

@ -1,21 +1,35 @@
# same as example1, but without explicit viztracer calls
import threading
import time
import time
# if we don't run with gil_load, we just skip it
import gil_load
try:
gil_load.init()
gil_load.start()
use_gil_load = True
except RuntimeError:
use_gil_load = False
def run():
def some_computation():
total = 0
for i in range(1_000_000):
total += i
return total
thread1 = threading.Thread(target=run)
thread2 = threading.Thread(target=run)
thread1 = threading.Thread(target=some_computation)
thread2 = threading.Thread(target=some_computation)
def main(args=None):
thread1.start()
thread2.start()
time.sleep(0.2)
for thread in [thread1, thread2]:
thread.join()
if use_gil_load:
gil_load.stop()
stats = gil_load.get()
print(gil_load.format(stats))
if __name__ == "__main__":
main()

View File

@ -7,6 +7,7 @@ import numpy as np
import gil_load
try:
gil_load.init()
gil_load.start()
use_gil_load = True
except RuntimeError:
use_gil_load = False
@ -16,24 +17,23 @@ N = 1024*1024*32
M = 4
x = np.arange(N, dtype='f8')
def run():
def some_numpy_computation():
total = 0
for i in range(M):
total += x.sum()
return total
if use_gil_load:
gil_load.start()
thread1 = threading.Thread(target=some_numpy_computation)
thread2 = threading.Thread(target=some_numpy_computation)
thread1 = threading.Thread(target=run)
thread2 = threading.Thread(target=run)
def main(args=None):
thread1.start()
thread2.start()
total = 0
for i in range(1_000_000):
for i in range(2_000_000):
total += i
for thread in [thread1, thread2]:
thread.join()
@ -42,3 +42,5 @@ def main(args=None):
gil_load.stop()
stats = gil_load.get()
print(gil_load.format(stats))
if __name__ == "__main__":
main()