circuits 3.2.3 Documentation

Release:

3.2.3

Date:

November 16, 2024

About

circuits is a Lightweight Event driven and Asynchronous Application Framework for the Python Programming Language with a strong Component Architecture.

circuits also includes a lightweight, high performance and scalable HTTP/WSGI compliant web server as well as various I/O and Networking components.

Got questions?

Examples

Hello

 1#!/usr/bin/env python
 2
 3"""circuits Hello World"""
 4
 5from circuits import Component, Event
 6
 7
 8class hello(Event):
 9    """hello Event"""
10
11
12class App(Component):
13    def hello(self):
14        """Hello Event Handler"""
15        print('Hello World!')
16
17    def started(self, component):
18        """
19        Started Event Handler
20
21        This is fired internally when your application starts up and can be used to
22        trigger events that only occur once during startup.
23        """
24        self.fire(hello())  # Fire hello Event
25
26        raise SystemExit(0)  # Terminate the Application
27
28
29App().run()

Download Source Code: hello.py:

Echo Server

 1#!/usr/bin/env python
 2
 3"""
 4Simple TCP Echo Server
 5
 6This example shows how you can create a simple TCP Server (an Echo Service)
 7utilizing the builtin Socket Components that the circuits library ships with.
 8"""
 9
10from circuits import Debugger, handler
11from circuits.net.sockets import TCPServer
12
13
14class EchoServer(TCPServer):
15    @handler('read')
16    def on_read(self, sock, data):
17        """
18        Read Event Handler
19
20        This is fired by the underlying Socket Component when there has been
21        new data read from the connected client.
22
23        ..note :: By simply returning, client/server socket components listen
24                  to ValueChagned events (feedback) to determine if a handler
25                  returned some data and fires a subsequent Write event with
26                  the value returned.
27        """
28        return data
29
30
31# Start and "run" the system.
32# Bind to port 0.0.0.0:8000
33
34
35app = EchoServer(('0.0.0.0', 8000))
36Debugger().register(app)
37app.run()

Download Source Code: echoserver.py:

Hello Web

 1#!/usr/bin/env python
 2
 3from circuits.web import Controller, Server
 4
 5
 6class Root(Controller):
 7    def index(self):
 8        """
 9        Index Request Handler
10
11        Controller(s) expose implicitly methods as request handlers.
12        Request Handlers can still be customized by using the ``@expose``
13        decorator. For example exposing as a different path.
14        """
15        return 'Hello World!'
16
17
18app = Server(('0.0.0.0', 8000))
19Root().register(app)
20app.run()

Download Source Code: helloweb.py:

More examples

Features

  • event driven

  • concurrency support

  • component architecture

  • asynchronous I/O components

  • no required external dependencies

  • full featured web framework (circuits.web)

  • coroutine based synchronization primitives

Requirements

Supported Platforms

  • Linux, FreeBSD, Mac OS X, Windows

  • Python 3.7, 3.8, 3.9, 3.10, 3.11, 3.12

  • pypy (the newer the better)

Installation

The simplest and recommended way to install circuits is with pip. You may install the latest stable release from PyPI with pip:

$ pip install circuits

If you do not have pip, you may use easy_install:

$ easy_install circuits

Alternatively, you may download the source package from the PyPi or the Downloads extract it and install using:

$ python setup.py install

Note

You can install the development version via pip install circuits==dev.

License

circuits is licensed under the MIT License.

Feedback

We welcome any questions or feedback about bugs and suggestions on how to improve circuits.

Let us know what you think about circuits. @pythoncircuits.

Do you have suggestions for improvement? Then please Create an Issue with details of what you would like to see. I’ll take a look at it and work with you to either incorporate the idea or find a better solution.

Community

There are also several places you can reach out to the circuits community:


Disclaimer

Whilst I (James Mills) continue to contribute and maintain the circuits project I do not represent the interests or business of my employer Facebook Inc. The contributions I make are of my own free time and have no bearing or relevance to Facebook Inc.

Documentation

Indices and tables