From 5c12a42d6299d8a415db1ebbbe5abda20862b44e Mon Sep 17 00:00:00 2001 From: Sam Pfeiffer Date: Wed, 1 Aug 2018 15:03:53 +1000 Subject: [PATCH] Add Python syntax coloring to code and fix typos --- README.md | 127 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 4bb42dc..dce7bef 100644 --- a/README.md +++ b/README.md @@ -8,30 +8,31 @@ that is checked out of the Curio GitHub repository at https://github.com/dabeaz/ ## High Level Overview (The Big Idea) Consider the following thread program involving a worker, a producer, -and queue:: - - import threading - import queue - import time - - def worker(q): - while True: - item = q.get() - if item is None: - break - print('Got:', item) - - def main(): - q = queue.Queue() - t = threading.Thread(target=worker, args=(q,)) - t.start() - for n in range(10): - q.put(n) - time.sleep(1) - q.put(None) - t.join() - - main() +and queue: +```python +import threading +import queue +import time + +def worker(q): + while True: + item = q.get() + if item is None: + break + print('Got:', item) + +def main(): + q = queue.Queue() + t = threading.Thread(target=worker, args=(q,)) + t.start() + for n in range(10): + q.put(n) + time.sleep(1) + q.put(None) + t.join() + +main() +``` In this code, there are blocking operations such as ``q.get()`` and ``time.sleep()``. This blocking is ultimately handled by the @@ -43,55 +44,57 @@ some event occurs that causes it to unblock. Thredo re-envisions threads by redirecting all blocking operations to an async library. The code looks mostly the same except that you use the `thredo` module. For example: +```python +import thredo + +def worker(q): + while True: + item = q.get() + if item is None: + break + print('Got:', item) + +def main(): + q = thredo.Queue() + t = thredo.spawn(worker, q) + for n in range(10): + q.put(n) + thredo.sleep(1) + q.put(None) + t.join() + +thredo.run(main) +``` - import thredo +The main reason you'd use ``thredo`` however is that it gives you extra +features such as thread groups, cancellation, and more. For example, +here's a more advanced version of the above code: +```python +import thredo - def worker(q): +def worker(q): + try: while True: item = q.get() - if item is None: - break print('Got:', item) + q.task_done() + except thredo.ThreadCancelled: + print('Worker cancelled') + +def main(): + q = thredo.Queue() + with thredo.ThreadGroup(wait=None) as workers: + for n in range(4): + workers.spawn(worker, q) - def main(): - q = thredo.Queue() - t = thredo.spawn(worker, q) for n in range(10): q.put(n) thredo.sleep(1) - q.put(None) - t.join() - - thredo.run(main) - -The main reason you'd use ``thredo`` however is that it gives you extra -features such as thread groups, cancellation, and more. For example, -here's a more advanced version of the above code:: - - import thredo - - def worker(q): - try: - while True: - item = q.get() - print('Got:', item) - q.task_done() - except thredo.ThreadCancelled: - print('Worker cancelled') - - def main(): - q = thredo.Queue() - with thredo.ThreadGroup(wait=None) as workers: - for n in range(4): - workers.spawn(worker, q) - - for n in range(10): - q.put(n) - thredo.sleep(1) - workers.join() + workers.join() - thredo.run(main) +thredo.run(main) +``` ## Examples