From 14f7f72a5456257fc424bb1c6e5791a23ee10052 Mon Sep 17 00:00:00 2001 From: jfsScience Date: Tue, 8 Jun 2021 10:00:42 +0200 Subject: [PATCH] threading --- gui/corey_one.py | 34 ++++++++++++++++++++++++++++++++++ gui/corey_two.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 gui/corey_one.py create mode 100644 gui/corey_two.py diff --git a/gui/corey_one.py b/gui/corey_one.py new file mode 100644 index 0000000..d90dd5c --- /dev/null +++ b/gui/corey_one.py @@ -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') \ No newline at end of file diff --git a/gui/corey_two.py b/gui/corey_two.py new file mode 100644 index 0000000..18603a7 --- /dev/null +++ b/gui/corey_two.py @@ -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') \ No newline at end of file