• Stars
    star
    4,001
  • Rank 10,885 (Top 0.3 %)
  • Language
    Python
  • License
    Other
  • Created about 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Good Curio!

Curio

Curio is a coroutine-based library for concurrent Python systems programming using async/await. It provides standard programming abstractions such as as tasks, sockets, files, locks, and queues as well as some advanced features such as support for structured concurrency. It works on Unix and Windows and has zero dependencies. You'll find it to be familiar, small, fast, and fun.

Important Notice: October 25, 2022

The Curio project is no longer making package releases. I'm more than happy to accept bug reports and may continue to work on it from time to time as the mood strikes. If you want the absolute latest version, you should vendor the source code from here. Curio has no dependencies other than the Python standard library. --Dave

Curio is Different

One of the most important ideas from software architecture is the "separation of concerns." This can take many forms such as utilizing abstraction layers, object oriented programming, aspects, higher-order functions, and so forth. However, another effective form of it exists in the idea of separating execution environments. For example, "user mode" versus "kernel mode" in operating systems. This is the underlying idea in Curio, but applied to "asynchronous" versus "synchronous" execution.

A fundamental problem with asynchronous code is that it involves a completely different evaluation model that doesn't compose well with ordinary applications or with other approaches to concurrency such as thread programing. Although the addition of "async/await" to Python helps clarify such code, "async" libraries still tend to be a confused mess of functionality that mix asynchronous and synchronous functionality together in the same environment--often bolting it all together with an assortment of hacks that try to sort out all of associated API confusion.

Curio strictly separates asynchronous code from synchronous code. Specifically, all functionality related to the asynchronous environment utilizes "async/await" features and syntax--without exception. Moreover, interactions between async and sync code is carefully managed through a small set of simple mechanisms such as events and queues. As a result, Curio is small, fast, and significantly easier to reason about.

A Simple Example

Here is a concurrent TCP echo server directly implemented using sockets:

# echoserv.py

from curio import run, spawn
from curio.socket import *

async def echo_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print('Server listening at', address)
    async with sock:
        while True:
            client, addr = await sock.accept()
            await spawn(echo_client, client, addr, daemon=True)

async def echo_client(client, addr):
    print('Connection from', addr)
    async with client:
         while True:
             data = await client.recv(100000)
             if not data:
                 break
             await client.sendall(data)
    print('Connection closed')

if __name__ == '__main__':
    run(echo_server, ('',25000))

If you've done network programming with threads, it looks almost identical. Moreover, it can handle thousands of clients even though no threads are being used inside.

Core Features

Curio supports standard synchronization primitives (events, locks, recursive locks, semaphores, and condition variables), queues, subprocesses, as well as running tasks in threads and processes. The task model fully supports cancellation, task groups, timeouts, monitoring, and other features critical to writing reliable code.

Read the official documentation for more in-depth coverage. The tutorial is a good starting point. The howto describes how to carry out common programming tasks.

Talks Related to Curio

Concepts related to Curio's design and general issues related to async programming have been described by Curio's creator, David Beazley, in various conference talks and tutorials:

Questions and Answers

Q: What is the point of the Curio project?

A: Curio is async programming, reimagined as something smaller, faster, and easier to reason about. It is meant to be both educational and practical.

Q: Is Curio implemented using asyncio?

A: No. Curio is a standalone library directly created from low-level I/O primitives.

Q: Is Curio meant to be a clone of asyncio?

A: No. Although Curio provides a significant amount of overlapping functionality, the API is different. Compatibility with other libaries is not a goal.

Q: Is Curio meant to be compatible with other async libraries?

A: No. Curio is a stand-alone project that emphasizes a certain software architecture based on separation of environments. Other libraries have largely ignored this concept, preferring to simply provide variations on the existing approach found in asyncio.

Q: Can Curio interoperate with other event loops?

A: It depends on what you mean by the word "interoperate." Curio's preferred mechanism of communication with the external world is a queue. It is possible to communicate between Curio, threads, and other event loops using queues.

Q: How fast is Curio?

A: Curio's primary goal is to be an async library that is minimal and understandable. Performance is not the primary concern. That said, in rough benchmarking of a simple echo server, Curio is more than twice as fast as comparable code using coroutines in asyncio or trio. This was last measured on OS-X using Python 3.9. Keep in mind there is a lot more to overall application performance than the performance of a simple echo server so your mileage might vary. However, as a runtime environment, Curio doesn't introduce a lot of extra overhead. See the examples/benchmark directory for various testing programs.

Q: What is the future of Curio?

A: Curio should be viewed as a library of basic programming primitives. At this time, it is considered to be feature-complete--meaning that it is not expected to sprout many new capabilities. It may be updated from time to time to fix bugs or support new versions of Python.

Q: Can I contribute?

A: Curio is not a community-based project seeking developers or maintainers. However, having it work reliably is important. If you've found a bug or have an idea for making it better, please file an issue.

Contributors

The following people contributed ideas to early stages of the Curio project: Brett Cannon, Nathaniel Smith, Alexander Zhukov, Laura Dickinson, and Sandeep Gupta.

Who

Curio is the creation of David Beazley (@dabeaz) who is also responsible for its maintenance. http://www.dabeaz.com

P.S.

If you want to learn more about concurrent programming more generally, you should come take a course!

More Repositories

1

python-cookbook

Code samples from the "Python Cookbook, 3rd Edition", published by O'Reilly & Associates, May, 2013.
Python
3,864
star
2

ply

Python Lex-Yacc
Python
2,717
star
3

sly

Sly Lex Yacc
Python
804
star
4

dataklasses

A different spin on dataclasses.
Python
782
star
5

bitey

Python
606
star
6

generators

Generator Tricks for Systems Programmers (Tutorial)
422
star
7

cluegen

Get a clue, get some code
Python
355
star
8

thredo

Thredo was an experiment - It's dead. Feel free to look around.
Python
338
star
9

blog

David Beazley's blog.
261
star
10

concurrencylive

Code from Concurrency Live - PyCon 2015
Python
157
star
11

python-distilled

Resources for Python Distilled (Book)
85
star
12

modulepackage

Materials for PyCon2015 Tutorial "Modules and Packages : Live and Let Die"
Python
74
star
13

wadze

Web Assembly Decoder - Zero Extras
Python
70
star
14

me-al

Meแบ—al - The Decorator
Python
64
star
15

pythonprog

Python
62
star
16

typemap

Typemap - The Annotator (TM)
Python
55
star
17

pylox

Python implementation of the Lox language from Robert Nystrom's Crafting Interpreters
Python
47
star
18

flail

Ball and Chain Decorators
Python
41
star
19

asm6502

A small 6502 assembler written in Python
Python
21
star
20

ranet

An Implementation of Raft in Janet
18
star
21

hoppy

9
star
22

raft_jun_2019

Raft Course 2019
Python
9
star
23

colonel

C Curio Kernel
Python
8
star
24

raft_dec19

Raft Project December 2019
Python
8
star
25

theater

Code for "The Problem with the Problem" talk, February 22, 2022.
Python
7
star
26

dabeaz.github.io

HTML
4
star
27

frothy

Frothy example from CSCI1730 Fall 2023
Racket
3
star
28

archive

Software Archive
3
star