threading
This commit is contained in:
parent
bfae474219
commit
14f7f72a54
34
gui/corey_one.py
Normal file
34
gui/corey_one.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import multiprocessing
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
start = time.perf_counter()
|
||||||
|
|
||||||
|
def do_something():
|
||||||
|
print("Start sleeping")
|
||||||
|
time.sleep(1)
|
||||||
|
print("awake")
|
||||||
|
|
||||||
|
threads = []
|
||||||
|
|
||||||
|
for _ in range(5):
|
||||||
|
p = threading.Thread(target=do_something)
|
||||||
|
p.start()
|
||||||
|
threads.append(p)
|
||||||
|
|
||||||
|
for t in threads :
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
#p1 = multiprocessing.Process(target=do_something)
|
||||||
|
#p2 = multiprocessing.Process(target=do_something)
|
||||||
|
#p1 = threading.Thread(target=do_something)
|
||||||
|
#p2 = threading.Thread(target=do_something)
|
||||||
|
#p1.start()
|
||||||
|
#p2.start()
|
||||||
|
#p1.join()
|
||||||
|
#p2.join()
|
||||||
|
|
||||||
|
ended = time.perf_counter()
|
||||||
|
|
||||||
|
|
||||||
|
print(f' Duration {round(ended - start,2)} s')
|
||||||
28
gui/corey_two.py
Normal file
28
gui/corey_two.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import concurrent.futures
|
||||||
|
import time
|
||||||
|
|
||||||
|
start = time.perf_counter()
|
||||||
|
|
||||||
|
def do_something(seconds):
|
||||||
|
print(f'Start sleeping {seconds} ')
|
||||||
|
time.sleep(seconds)
|
||||||
|
return f'done sleeping for {seconds}'
|
||||||
|
|
||||||
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
|
secs = [5,4,3,2,1]
|
||||||
|
# results = [executor.submit(do_something,sec) for sec in secs]
|
||||||
|
results = executor.map(do_something,secs)
|
||||||
|
|
||||||
|
# for f in concurrent.futures.as_completed(results):
|
||||||
|
# print(f.result())
|
||||||
|
|
||||||
|
|
||||||
|
for result in results:
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ended = time.perf_counter()
|
||||||
|
|
||||||
|
|
||||||
|
print(f' Duration {round(ended - start,2)} s')
|
||||||
Loading…
Reference in New Issue
Block a user