Last active
June 20, 2020 07:05
-
-
Save bobby569/687e2de2fd79784b81197580a5a6c3c1 to your computer and use it in GitHub Desktop.
Multithreading examples in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| To count up for a global variable. | |
| """ | |
| from threading import Thread, Lock | |
| var = 0 | |
| lock = Lock() | |
| class IncrementThread(Thread): | |
| def run(self): | |
| global var, lock | |
| with lock: | |
| for _ in range(10000): | |
| var += 1 | |
| def use_increment_thread(): | |
| threads = [] | |
| for _ in range(4): | |
| t = IncrementThread() | |
| threads.append(t) | |
| t.start() | |
| for t in threads: | |
| t.join() | |
| print("var: %d" % var) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Test responses from different websites. | |
| """ | |
| import time | |
| import urllib.request | |
| from threading import Thread | |
| class GetUrlThread(Thread): | |
| def __init__(self, url): | |
| super().__init__() | |
| self.url = url | |
| def run(self): | |
| res = urllib.request.urlopen(self.url) | |
| print(self.url, res.getcode()) | |
| def get_response(): | |
| urls = [ | |
| 'https://www.google.com', | |
| 'https://www.amazon.com/', | |
| 'https://www.ebay.com', | |
| 'https://www.alibaba.com', | |
| 'https://www.thumbtack.com/' | |
| ] | |
| start = time.time() | |
| threads = [] | |
| for url in urls: | |
| t = GetUrlThread(url) | |
| threads.append(t) | |
| t.start() | |
| for t in threads: | |
| t.join() | |
| print("Time cost: %.3f" % (time.time() - start)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Producer and consumer pattern. | |
| """ | |
| import random | |
| import time | |
| from threading import Thread, Condition | |
| queue = [] | |
| MAX_NUM = 10 | |
| condition = Condition() | |
| class ProducerThread(Thread): | |
| def run(self): | |
| global queue | |
| while True: | |
| condition.acquire() | |
| if len(queue) == MAX_NUM: | |
| print("Queue full") | |
| condition.wait() | |
| print("Queue available") | |
| num = random.choice(range(5)) | |
| queue.append(num) | |
| print("Produced", num) | |
| condition.notify() | |
| condition.release() | |
| time.sleep(random.random()) | |
| class ConsumerThread(Thread): | |
| def run(self): | |
| global queue | |
| while True: | |
| condition.acquire() | |
| if not queue: | |
| print("Nothing in queue") | |
| condition.wait() | |
| print("Producer added something") | |
| num = queue.pop(0) | |
| print("Consumed", num) | |
| condition.notify() | |
| condition.release() | |
| time.sleep(random.random()*2) | |
| ProducerThread().start() | |
| ConsumerThread().start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Producer and Consumer implemented using a build-in thread-safe Queue. | |
| """ | |
| import random | |
| import time | |
| from threading import Thread | |
| from queue import Queue | |
| queue = Queue(10) | |
| class ProducerThread(Thread): | |
| def run(self): | |
| global queue | |
| while True: | |
| num = random.choice(range(5)) | |
| queue.put(num) | |
| print("Produced", num) | |
| time.sleep(random.random()) | |
| class ConsumerThread(Thread): | |
| def run(self): | |
| global queue | |
| while True: | |
| num = queue.get() | |
| queue.task_done() | |
| print("Consumed", num) | |
| time.sleep(random.random()) | |
| ProducerThread().start() | |
| ConsumerThread().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment