From be98abd3b52826499bdcf938128c58c347b5137c Mon Sep 17 00:00:00 2001 From: Mia von Steinkirch Date: Wed, 4 Mar 2020 10:12:53 -0800 Subject: [PATCH] add some multiprocess and race condition examples --- Concurrence_examples/daemon_example.py | 39 +++++++++++++++++++++++++ Concurrence_examples/logging_example.py | 15 ++++++++++ Concurrence_examples/race_coditions.py | 5 ++-- Concurrence_examples/thread_example.py | 2 +- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Concurrence_examples/daemon_example.py create mode 100644 Concurrence_examples/logging_example.py diff --git a/Concurrence_examples/daemon_example.py b/Concurrence_examples/daemon_example.py new file mode 100644 index 0000000..3f5721d --- /dev/null +++ b/Concurrence_examples/daemon_example.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import time +import sys +import multiprocessing + + +def daemon(): + p = multiprocessing.current_process() + + print('Starting: {}, {}'.format(p.name, p.pid)) + sys.stdout.flush() + time.sleep(2) + print('Exiting : {}, {}'.format(p.name, p.pid)) + + sys.stdout.flush() + + +def non_daemon(): + p = multiprocessing.current_process() + + print('Starting: {}, {}'.format(p.name, p.pid)) + sys.stdout.flush() + print('Exiting : {}, {}'.format(p.name, p.pid)) + + sys.stdout.flush() + + + +if __name__ == '__main__': + d = multiprocessing.Process(name='daemon', target=daemon) + d.daemon = True + + n = multiprocessing.Process(name='non-daemon', target=non_daemon) + n.daemon = False + + d.start() + time.sleep(1) + n.start() diff --git a/Concurrence_examples/logging_example.py b/Concurrence_examples/logging_example.py new file mode 100644 index 0000000..9af8b4c --- /dev/null +++ b/Concurrence_examples/logging_example.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import multiprocessing +import logging +import sys + +def worker(): + print 'Doing some work' + sys.stdout.flush() + +if __name__ == '__main__': + multiprocessing.log_to_stderr(logging.DEBUG) + p = multiprocessing.Process(target=worker) + p.start() + p.join() \ No newline at end of file diff --git a/Concurrence_examples/race_coditions.py b/Concurrence_examples/race_coditions.py index d7ce3a9..c9a6234 100644 --- a/Concurrence_examples/race_coditions.py +++ b/Concurrence_examples/race_coditions.py @@ -3,17 +3,16 @@ import threading x = 0 - COUNT = 10000000 def foo(): global x - for i in xrange(COUNT): + for i in range(COUNT): x += 1 def bar(): global x - for i in xrange(COUNT): + for i in range(COUNT): x -= 1 t1 = threading.Thread(target=foo) diff --git a/Concurrence_examples/thread_example.py b/Concurrence_examples/thread_example.py index a04d2e3..1485939 100644 --- a/Concurrence_examples/thread_example.py +++ b/Concurrence_examples/thread_example.py @@ -8,7 +8,7 @@ import threading def worker(n): sleep = random.randrange(1, 10) time.sleep(sleep) - print("Worker {}: sleeping for {} seconds.".format(n, sleep)) + print("Worker {} from {}: sleeping for {} seconds.".format(n, threading.get_ident(), sleep)) for i in range(5):