Skip to main content

engine

Core event loop implementation.

This package contains a configurable event loop and a set of hooks that allow for async networking, time based scheduling, and coroutine management. Below are samples of using each of the major components. Use caution. This project is still very much in its infancy.

Example Usage

Async Networking

# Create a non-blocking socket server that logs data and closes connections.

import functools
import socket

from asyncdef.engine.core import Engine
from asyncdef.engine.processors.selector import Selector

# Generate a non-blocking socket listener.
sock = socket.socket()
sock.setblocking(0)
sock.bind(('localhost', 9898))
sock.listen()


def handle_data(selector, conn):
    """Triggered on data waiting for read."""
    data = conn.recv(4096)
    print('Got data {}.'.format(data))
    selector.remove_reader(conn)
    conn.close()


def handle_connection(selector, listening_socket):
    """Triggered on new incoming connection."""
    conn, addr = listening_socket.accept()
    conn.setblocking(0)
    selector.add_reader(
        conn,
        functools.partial(handle_data, selector),
    )

# Create an instance of the selector and event loop.
selector = Selector()
loop = Engine(selector)

# Set up the socket polling.
selector.add_reader(sock, functools.partial(handle_connection, selector))

# Start the event loop.
loop.start()

Time Scheduling

# Schedule a function to run in five seconds.
import functools

from asyncdef.engine.core import Engine
from asyncdef.engine.processors.time import Time


def print_and_stop(loop):
    print("Stopping the loop.")
    loop.stop()

time = Time()
loop = Engine(time)
handle = loop.defer_for(5, functools.partial(print_and_stop, loop))

loop.start()

Coroutine Management

import functools
from concurrent.futures import Future

from asyncdef.engine.core import Engine
from asyncdef.engine.processors.coroutine import Coroutine
from asyncdef.engine.processors.time import Time


class AwaitableFuture(Future):

    """An awaitable version of the standard lib Future."""

    def __await__(self):
        """Return self until resolved."""
        while self._state != 'FINISHED':

            yield self

        return self.result()


async def wait_for_future(loop, future):
    value = await future
    print('Got value {}. Stopping the loop.'.format(value))
    loop.stop()


def resolve_future(future):
    print('Resolving future.')
    future.set_result(True)

time = Time()
coromgr = Coroutine()
loop = Engine(coromgr, time)

future = AwaitableFuture()
time.defer_for(5, functools.partial(resolve_future, future))
handle = coromgr.add(wait_for_future(loop, future))

loop.start()

Testing

All tests suites are paired one-to-one with the module they test and live directly adjacent to that same module. All tests are expected to pass for Python 3.5 and above. To run tests use tox with the included tox.ini file or create a virtualenv and install the ‘[testing]’ extras.

License

Copyright 2016 Kevin Conway

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Contributing

Firstly, if you’re putting in a patch then thank you! Here are some tips for getting your patch merged:

Style

As long as the code passes the PEP8 and PyFlakes gates then the style is acceptable.

Docs

The PEP257 gate will check that all public methods have docstrings. If you’re adding something new, like a helper function, try out the napoleon style of docstrings.

Tests

Make sure the patch passes all the tests. If you’re adding a new feature don’t forget to throw in a test or two. If you’re fixing a bug then definitely add at least one test to prevent regressions.

Metadata

Release files for asyncdef.engine 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncdef.engine 0.1.1
File Size Uploaded Provenance
asyncdef.engine-0.1.1.tar.gz 15.2 kB Without provenance Details

Release files / asyncdef.engine-0.1.1.tar.gz

Download URL asyncdef.engine-0.1.1.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a7e02139087f1ac6a65e3f11fb7417c245d801a21197bc08e813e49b5cdaae34
BLAKE2b-256 checksum
How to use checksums
4eabce99c24f74074173dcfbc3e7557a64a0ffcaf0d071b48011cf5108649126
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for asyncdef.engine 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page