Uvio Documentation Release Undefined

Uvio Documentation Release Undefined

uvio Documentation Release undefined Sean Ross-Ross August 28, 2016 Contents 1 Motivation 3 2 Kitchen Sink 5 3 Why not AsyncIO? 7 4 Features 9 4.1 The UV Event Loop...........................................9 4.2 Workers.................................................. 11 4.3 Coroutines................................................ 11 4.4 Subprocess................................................ 12 4.5 File System................................................ 12 4.6 Network................................................. 13 4.7 Pipes................................................... 14 4.8 UVIO API refrence............................................ 16 5 Indices and tables 17 Python Module Index 19 i ii uvio Documentation, Release undefined 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, named pipes, threadding, subprocesses and more. uvio is based off of libuv This is a complete replacement for python’s asyncio module. Contents 1 uvio Documentation, Release undefined 2 Contents CHAPTER 1 Motivation • Because I can • Provide async filesystem objects • Better subprocess support 3 uvio Documentation, Release undefined 4 Chapter 1. Motivation CHAPTER 2 Kitchen Sink Example: import uvio @uvio.sync async def main(): if __name__ == '__main__': main() 5 uvio Documentation, Release undefined 6 Chapter 2. Kitchen Sink CHAPTER 3 Why not AsyncIO? • We don’t need to care about the event loop. In asyncio, the event loop is very prominent. A user does not need to care about the type of event loop they are using • Don’t need to pass loop to every function. In many of the examples <https://docs.python.org/3/library/asyncio- task.html#example-coroutine-displaying-the-current-date> in asyncio this is the case. • handles keep loop running don’t need to run_forever or run 7 uvio Documentation, Release undefined 8 Chapter 3. Why not AsyncIO? CHAPTER 4 Features • Full-featured event loop backed by epoll, kqueue, IOCP, event ports. • Asynchronous TCP and UDP sockets • Asynchronous DNS resolution • Asynchronous file and file system operations • IPC with socket sharing, using Unix domain sockets or named pipes (Windows) • Child processes • Thread pool • TODO: File system events • TODO: ANSI escape code controlled TTY • TODO: Signal handling • TODO: High resolution clock • TODO: Threading and synchronization primitives Contents: 4.1 The UV Event Loop 4.1.1 Event loop examples Execute async fuctions with uvio.sync() Often you don’t care about the event loop if that is the case and you just want to get things done use the uvio.sync() decorator: import uvio @uvio.sync async def main(args): await uvio.sleep(1) print("OK!") if __name__ == '__main__': main() 9 uvio Documentation, Release undefined Display the current date with get_current_loop() with asyncio, the loop parameter often needs to be a part of the function call args with uvio use get_current_loop wich will return the loop running the current coroutine: import uvio @uvio.sync async def main(args): loop = await uvio.get_current_loop() print(loop) Hello World with next_tick() Example using the Loop.next_tick() method to schedule a callback. The callback displays "Hello World" and then stops the event loop: import uvio def hello_world(loop): print('Hello World') loop= uvio.get_default_loop() # Schedule a call to hello_world() loop.next_tick(hello_world, loop) # Blocking call interrupted by loop.stop() loop.run() loop.close() Display the current date with schedule() Example of callback displaying the current date every second. The callback uses the BaseEventLoop.call_later() method to reschedule itself during 5 seconds, and then stops the event loop: import uvio import datetime async def display_date(end_time): print(datetime.datetime.now()) if (datetime.datetime.now() + 1.0) < end_time: await schedule(1, display_date, end_time) loop = uvio.get_default_loop() # Schedule the first call to display_date() end_time = datetime.datetime.now() + 5.0 loop.next_tick(display_date, end_time) # Blocking call will exit when all tasks are finished 10 Chapter 4. Features uvio Documentation, Release undefined loop.run() loop.close() 4.2 Workers 4.3 Coroutines 4.3.1 Coroutines Examples Parallel execution of tasks Example import uvio from datetime import datetime @uvio.sync async def display_date(): end_time = datetime.now() + timedelta(seconds=5) while True: print(datetime.now()) if (datetime.now() + timedelta(seconds=5)) >= end_time: break await uvio.sleep(1) display_date() Parallel execution of tasks Example executing 3 tasks (A, B, C) in parallel: import uvio async def factorial(name, number): f = 1 for i in range(2, number+1): print("Task %s: Compute factorial(%s)..." % (name, i)) await uvio.sleep(1) f *= i print("Task %s: factorial(%s) = %s" % (name, number, f)) loop = uvio.get_default_loop() loop.next_tick(factorial("A", 2)) loop.next_tick(factorial("B", 3)) loop.next_tick(factorial("C", 4)) loop.run() loop.close() 4.2. Workers 11 uvio Documentation, Release undefined 4.4 Subprocess 4.4.1 Subprocess examples Example of a subprocess protocol using to get the output of a subprocess and to wait for the subprocess exit. The subprocess is created by the uvio.subprocess.Popen method: import uvio import sys @uvio.sync(timout=10) async def get_date(): code = 'import datetime; print(datetime.datetime.now())' # Create the subprocess controlled by the protocol DateProtocol, # redirect the standard output into a pipe process = await uvio.process.Popen( [sys.executable, '-c', code], stdin=None, stderr=None, stdout=uvio.process.PIPE ) # Pipe output into byte stream line = await process.stdout.readline() # Wait for the subprocess exit using the process_exited() method # of the protocol print('Exit Status', await process.returncode) # Read the output which was collected by the pipe_data_received() # method of the protocol data = output.getvalue() return data.decode('ascii').rstrip() print("Current date: %s" % get_date()) 4.5 File System 4.5.1 File System examples Reading Files Open a file for reading and read the data: async with uvopen(test_data,"r") as fd: data= await fd.read() 12 Chapter 4. Features uvio Documentation, Release undefined 4.6 Network 4.6.1 Network examples TCP echo client example import uvio message = 'Hello World!' @uvio.sync async def main(): socket = await uvio.net.connect('127.0.0.1', 8888) socket.write(message.encode()) print('Data sent: {!r}'.format(self.message)) @socket.data def data_received(data): print('Data received: {!r}'.format(data.decode())) @socket.end def connection_lost(): print('The server closed the connection') print('Stop the event loop by closing the all open handles') print('One could aslo call loop.close()') socket.close() TCP echo server example import uvio async def handler(socket): print('Connection from {}'.format(socket.peername())) @socket.data def data_received(data): message = data.decode() print('Data received: {!r}'.format(message)) print('Send: {!r}'.format(message)) socket.write(data) print('Close the client socket') socket.close() @uvio.sync async def main(): # Each client connection will create a new protocol instance server = await uvio.net.listen(handler, '127.0.0.1', 8888) 4.6. Network 13 uvio Documentation, Release undefined print('Serving on {}'.format(server.getsockname())) Get HTTP Headers example: import uvio import urllib.parse import sys @uvio.sync(timeout=3.0) async def print_http_headers(url): url = urllib.parse.urlsplit(url) if url.scheme == 'https': # Not implemented raise NotImplementedError("https") else: socket = await uvio.net.connect(url.hostname, 80) query = ('HEAD {path} HTTP/1.0\r\n' 'Host: {hostname}\r\n' '\r\n').format(path=url.path or '/', hostname=url.hostname) writer.write(query.encode('latin-1')) while True: line = await socket.readline() if not line: break line = line.decode('latin1').rstrip() if line: print('HTTP header> %s' % line) # Ignore the body, close the socket socket.close() def main(): url = sys.argv[1] print_http_headers(url) 4.7 Pipes Pipes are implemented as linux domain sockets or windows named pipes depending on what system you are running on. 4.7.1 Pipe examples Pipe echo client example import uvio 14 Chapter 4. Features uvio Documentation, Release undefined message = 'Hello World!' def handler(socket): loop = asyncio.get_event_loop() message = 'Hello World!' @uvio.sync async def main(): socket = await uvio.pipes.connect('windows_pipe_or_unix_domain.sock') socket.write(message.encode()) print('Data sent: {!r}'.format(self.message)) @socket.data def data_received(data): print('Data received: {!r}'.format(data.decode())) @socket.end def connection_lost(): print('The server closed the connection') print('Stop the event loop') socket.close() Pipes echo server example import uvio async def handler(socket): print('Connection from {}'.format(socket.getpeername())) @socket.data def data_received(data): message = data.decode() print('Data received: {!r}'.format(message)) print('Send: {!r}'.format(message)) socket.write(data) print('Close the client socket') socket.close() @uvio.sync async def main(): # Each client connection will create a new protocol instance server = await uvio.pipes.listen(handler, 'windows_pipe_or_unix_domain.sock') print('Serving on {}'.format(server.getsockname())) 4.7. Pipes 15 uvio Documentation, Release undefined 4.8 UVIO API refrence 4.8.1 The Event Loop 4.8.2 The Stream Object The stream object! This is the most used object in libuv 4.8.3 Coroutines 4.8.4 Subprocess Module 4.8.5 Network Module 4.8.6 FS Module 4.8.7 Pipes Module 16 Chapter 4. Features CHAPTER 5 Indices and tables • genindex • modindex • search 17 uvio Documentation, Release undefined 18 Chapter 5. Indices and tables Python Module Index u uvio, 16 uvio.fs, 16 uvio.net, 16 uvio.pipes, 16 uvio.process, 16 19 uvio Documentation, Release undefined 20 Python Module Index Index U uvio (module), 16 uvio.fs (module), 16 uvio.net (module), 16 uvio.pipes (module), 16 uvio.process (module), 16 21.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    25 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us