Giter VIP home page Giter VIP logo

pygrunn2014-snippets's Introduction

pygrunn2014-snippets

Code snippets from my talk at PyGrunn 2014: A first look at asyncio

Slides for the talk available at http://isnomore.net/f/pygrunn-2014-asyncio.pdf

Anagram client/server

This is a very simple example:

  • A server that listens on a port for client connections; it reads a sentence, "calculates" an anagram of each of its words and writes that back to the client.

  • A client that connects to the server, sends it a sentence, waits for it to return an anagram version of the sentence, and prints it.

Usage:

Run the server:

[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_server.py

Then run several instances of the client, passing the sentence (in inverted commas) as a command-line parameter:

[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_client.py 'To be or not to be'


[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_client.py 'My kingdom for a horse'

The idea is that the server will receive the first sentence first, but will take longer to process it (see the _sleep_time generator). It will then receive, process and answer the second request before answering the first one, therefore exemplifying asynchronous I/O.

The expected output is something like this:

[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_server.py
Got: To be or not to be
Got: My kingdom for a horse
Wrote: My ogndmki for a hsero
Wrote: oT be ro nto ot eb

[-----]

[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_client.py 'To be or not to be'
oT be ro nto ot eb

[-----]

[rbp@stamppotje:pygrunn2014-snippets]$ python3 anagram_client.py 'My kingdom for a horse'
My ogndmki for a hsero

pygrunn2014-snippets's People

Contributors

rbp avatar

Watchers

 avatar  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.