From 5224dc91ca3007e799ac6da561a93187661fe4e2 Mon Sep 17 00:00:00 2001 From: Mia von Steinkirch Date: Tue, 3 Mar 2020 23:24:17 -0800 Subject: [PATCH] add all concurrence examples --- .../concurrence_future_example.py | 2 ++ Concurrence_examples/concurrent_tasks.py | 2 ++ .../concurrent_tasks_with_math.py | 2 ++ Concurrence_examples/getenv_example.py | 3 ++- .../multiprocessing_example.py | 4 +++- Concurrence_examples/pool_example.py | 10 ++++++++++ Concurrence_examples/race_coditions.py | 2 ++ Concurrence_examples/safe_thread_example.py | 2 ++ Concurrence_examples/thread_example.py | 2 ++ Concurrence_examples/threadpool_example.py | 18 ++++++++++++++++++ Concurrence_examples/threads_with_queues.py | 2 ++ Concurrence_examples/unsafe_thread_example.py | 2 ++ 12 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 Concurrence_examples/pool_example.py create mode 100644 Concurrence_examples/threadpool_example.py diff --git a/Concurrence_examples/concurrence_future_example.py b/Concurrence_examples/concurrence_future_example.py index 90d9045..09f83b6 100644 --- a/Concurrence_examples/concurrence_future_example.py +++ b/Concurrence_examples/concurrence_future_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import random import logging import concurrent.futures diff --git a/Concurrence_examples/concurrent_tasks.py b/Concurrence_examples/concurrent_tasks.py index b63dee5..7a6e143 100644 --- a/Concurrence_examples/concurrent_tasks.py +++ b/Concurrence_examples/concurrent_tasks.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import os import time import threading diff --git a/Concurrence_examples/concurrent_tasks_with_math.py b/Concurrence_examples/concurrent_tasks_with_math.py index 61bfe0d..04a1753 100644 --- a/Concurrence_examples/concurrent_tasks_with_math.py +++ b/Concurrence_examples/concurrent_tasks_with_math.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import os import time import threading diff --git a/Concurrence_examples/getenv_example.py b/Concurrence_examples/getenv_example.py index 0d7e350..d815339 100644 --- a/Concurrence_examples/getenv_example.py +++ b/Concurrence_examples/getenv_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import time from gevent.pool import Pool from gevent import monkey @@ -20,4 +22,3 @@ pool.join() end_time = time.time() print("Time for GreenSquirrel: %ssecs" % (end_time - start_time)) -g \ No newline at end of file diff --git a/Concurrence_examples/multiprocessing_example.py b/Concurrence_examples/multiprocessing_example.py index 282f184..fda3c45 100644 --- a/Concurrence_examples/multiprocessing_example.py +++ b/Concurrence_examples/multiprocessing_example.py @@ -1,6 +1,8 @@ -import multiprocessing +#!/usr/bin/env python3 + import time import random +import multiprocessing def worker(n): diff --git a/Concurrence_examples/pool_example.py b/Concurrence_examples/pool_example.py new file mode 100644 index 0000000..ebcb3c4 --- /dev/null +++ b/Concurrence_examples/pool_example.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 + +from multiprocessing import Pool + +def f(x): + return x*x + +if __name__ == '__main__': + p = Pool(5) + print(p.map(f, [1, 2, 3])) diff --git a/Concurrence_examples/race_coditions.py b/Concurrence_examples/race_coditions.py index e50e360..d7ce3a9 100644 --- a/Concurrence_examples/race_coditions.py +++ b/Concurrence_examples/race_coditions.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import threading x = 0 diff --git a/Concurrence_examples/safe_thread_example.py b/Concurrence_examples/safe_thread_example.py index e222592..9d7389c 100644 --- a/Concurrence_examples/safe_thread_example.py +++ b/Concurrence_examples/safe_thread_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import threading counter = 0 diff --git a/Concurrence_examples/thread_example.py b/Concurrence_examples/thread_example.py index 572c550..a04d2e3 100644 --- a/Concurrence_examples/thread_example.py +++ b/Concurrence_examples/thread_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import time import random import threading diff --git a/Concurrence_examples/threadpool_example.py b/Concurrence_examples/threadpool_example.py new file mode 100644 index 0000000..a48e021 --- /dev/null +++ b/Concurrence_examples/threadpool_example.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +from time import sleep +from concurrent.futures import ThreadPoolExecutor + + +def return_after_5_secs(message): + sleep(5) + return message + +pool = ThreadPoolExecutor(3) + + +future = pool.submit(return_after_5_secs, ("hello")) +print(future.done()) +sleep(5) +print(future.done()) +print(future.result()) \ No newline at end of file diff --git a/Concurrence_examples/threads_with_queues.py b/Concurrence_examples/threads_with_queues.py index dfa8083..5d30095 100644 --- a/Concurrence_examples/threads_with_queues.py +++ b/Concurrence_examples/threads_with_queues.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import time from queue import Queue from threading import Thread diff --git a/Concurrence_examples/unsafe_thread_example.py b/Concurrence_examples/unsafe_thread_example.py index 132f0ce..9aab375 100644 --- a/Concurrence_examples/unsafe_thread_example.py +++ b/Concurrence_examples/unsafe_thread_example.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import time import threading