diff --git a/Concurrence_examples/concurrent_tasks_with_math.py b/Concurrence_examples/concurrent_tasks_with_math.py new file mode 100644 index 0000000..61bfe0d --- /dev/null +++ b/Concurrence_examples/concurrent_tasks_with_math.py @@ -0,0 +1,47 @@ +import os +import time +import threading +import multiprocessing + +NUM_WORKERS = 4 + +def run_numbers(): + print("PID: %s, Process Name: %s, Thread Name: %s" % ( + os.getpid(), + multiprocessing.current_process().name, + threading.current_thread().name) + ) + x = 0 + while x < 10000000: + x += 1 + +start_time = time.time() + +for _ in range(NUM_WORKERS): + run_numbers() + +end_time = time.time() + +print("Serial time=", end_time - start_time) + + +start_time = time.time() + +threads = [threading.Thread(target=run_numbers) for _ in range(NUM_WORKERS)] +[thread.start() for thread in threads] +[thread.join() for thread in threads] + +end_time = time.time() + +print("Threads time=", end_time - start_time) + + +start_time = time.time() + +processes = [multiprocessing.Process(target=run_numbers) for _ in range(NUM_WORKERS)] +[process.start() for process in processes] +[process.join() for process in processes] +end_time = time.time() + + +print("Parallel time=", end_time - start_time) \ No newline at end of file diff --git a/Concurrence_examples/getenv_example.py b/Concurrence_examples/getenv_example.py new file mode 100644 index 0000000..0d7e350 --- /dev/null +++ b/Concurrence_examples/getenv_example.py @@ -0,0 +1,23 @@ +import time +from gevent.pool import Pool +from gevent import monkey + +# Note that you can spawn many workers with gevent since the cost of creating and switching is very low +NUM_WORKERS = 4 + +# Monkey-Patch socket module for HTTP requests +monkey.patch_socket() + +start_time = time.time() + +pool = Pool(NUM_WORKERS) +for address in WEBSITE_LIST: + pool.spawn(check_website, address) + +# Wait for stuff to finish +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/threads_with_queues.py b/Concurrence_examples/threads_with_queues.py new file mode 100644 index 0000000..dfa8083 --- /dev/null +++ b/Concurrence_examples/threads_with_queues.py @@ -0,0 +1,34 @@ +import time +from queue import Queue +from threading import Thread + +NUM_WORKERS = 4 +task_queue = Queue() + +def worker(): + # Constantly check the queue for addresses + while True: + address = task_queue.get() + run_function(address) + + # Mark the processed task as done + task_queue.task_done() + +start_time = time.time() + +# Create the worker threads +threads = [Thread(target=worker) for _ in range(NUM_WORKERS)] + +# Add the websites to the task queue +[task_queue.put(item) for item in SOME_LIST] + +# Start all the workers +[thread.start() for thread in threads] + +# Wait for all the tasks in the queue to be processed +task_queue.join() + + +end_time = time.time() + +print("Time: %ssecs" % (end_time - start_time)) \ No newline at end of file