Code Monkey home page Code Monkey logo

asyncio-practice's Introduction

asyncio practice

Requires Python 3.5 or higher.

Janken

Install pyee.

$ pip install pyee

Start Janken server.

$ python janken/server.py

Run multiple Janken clients.

$ seq 3 | xargs -I{} -P 3 python janken/client.py

asyncio-practice's People

Contributors

hoto17296 avatar

Watchers

James Cloos avatar  avatar  avatar

asyncio-practice's Issues

データを Pickle で送ろうとするとクライアントがコケる

diff --git a/janken/message.py b/janken/message.py
index 9ffd599..734b486 100644
--- a/janken/message.py
+++ b/janken/message.py
@@ -1,5 +1,5 @@
 import re
-import json
+import pickle
 import asyncio
 from pyee import EventEmitter

@@ -22,19 +22,15 @@ class MessageConnection(EventEmitter):
     def send(self, event, *args, **kwargs):
         assert(self.writer)
         assert(type(event) is str and re.compile(r'^\w+$').match(event))
-        message = '%s\t%s\n' % (event, json.dumps([args, kwargs]))
-        self.writer.write(message.encode())
+        message = pickle.dumps((event, args, kwargs)) + b'\n'
+        self.writer.write(message)

     async def listen(self):
         assert(self.reader)
-        pattern = re.compile(r'^(\w+?)\t(.*)\n$')
         while True:
             message = await self.reader.readline()
             if message:
-                matched = pattern.match(message.decode())
-                assert(matched)
-                event, data = matched.groups()
-                args, kwargs = json.loads(data)
+                event, args, kwargs = pickle.loads(message)
                 self.emit(event, *args, **kwargs)
             else:
                 self.emit('disconnect')
Traceback (most recent call last):
  File "janken/client.py", line 41, in <module>
    loop.run_until_complete(connection.connect('localhost', 8888))
  File "~/.anyenv/envs/pyenv/versions/3.6.1/lib/python3.6/asyncio/base_events.py", line 466, in run_until_complete
    return future.result()
  File "asyncio-practice/janken/message.py", line 20, in connect
    await self.listen()
  File "asyncio-practice/janken/message.py", line 33, in listen
    event, args, kwargs = pickle.loads(message)
EOFError: Ran out of input

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.