GithubHelp home page GithubHelp logo

uvio's Introduction

uvio

Alternative for python asyncio based on libuv.

Just like asyncio, this module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.

Installation

Install libuv. sorry no conda package yet

git clone https://github.com/srossross/uvio
python setup.py install

Network

from uvio import run
from uvio.net import connect, listen

async def echo_server(server, socket):

    @socket.data
    def echo(buf):
        socket.write(b"echo: " + buf)

    socket.resume()


@run(timout=1)
async def main():

    server = await listen("127.0.0.1", 80, echo_server)

    client = await connect("127.0.0.1", 80)

    @client.data
    def msg(client, data):
        print("The server echoe me", data)

        server.close()
        client.close()


    await client.write(b"this is a test")


File IO

from uvio import fs

async with fs.open('test_write.txt', 'w') as fd:
    n = await fd.write(b"Hello World")

Subprocess

from uvio.process import Popen, PIPE
echo = await Popen(['echo', 'hello world'], stdout=PIPE)

@echo.stdout.data
def output(buf):
    print("program output:", buf)

print('echo exited with status code', await echo.returncode)

Pipe output from one process to another

from uvio.process import Popen, PIPE
echo = await Popen(['echo', 'hello world'], stdout=PIPE)
cat = await Popen(['cat', '-'], stdin=PIPE, stdout=sys.stdout)

echo.stdout.pipe(cat.stdint)

print('cat exited with status code', await cat.returncode)

Worker Threads

For any other calls that are not covered by this library you can wrap blocing calls in a worker.


from libuv
def blocking(i, name):
    # This is not an async sleep! This will block the program

    # This will raise TypeError if i is not an int
    time.sleep(int(i))

    value = "Hello: {}".format(name)
    return value


async def callback():
    nonlocal called

    try:
        value = await worker(blocking, 'Fail', "Tester")
    except TypeError:
        print("caught exception in a thread! cool.")

    value = await worker(blocking, 1, "Tester")

uvio's People

Contributors

srossross avatar

Stargazers

Frans Oilinki avatar Saúl Ibarra Corretgé avatar

Watchers

James Cloos avatar Yoong Hor Meng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.