Created
September 18, 2015 13:28
-
-
Save dharhas/4cce183e954dcbb36c83 to your computer and use it in GitHub Desktop.
Attempt to use pulsar to do rpc and async download concurrently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from pulsar.apps import rpc, wsgi | |
from pulsar.utils.httpurl import JSON_CONTENT_TYPES | |
@asyncio.coroutine | |
def long_download(request, delay=1): | |
print('sleeping for: {0} seconds'.format(delay)) | |
yield from asyncio.sleep(delay) | |
print('{0} seconds task is finished'.format(delay)) | |
class MyRpc(rpc.JSONRPC): | |
def rpc_ping(self, request): | |
return 'pong' | |
rpc_long_download = rpc.rpc_method(long_download) | |
class Site(wsgi.LazyWsgi): | |
'''WSGI handler for the RPC server''' | |
def setup(self, environ): | |
'''Called once to setup the list of wsgi middleware.''' | |
middleware = wsgi.Router('/', post=MyRpc(), | |
accept_content_types=JSON_CONTENT_TYPES) | |
response = [wsgi.GZipMiddleware(200)] | |
return wsgi.WsgiHandler(middleware=[wsgi.wait_for_body_middleware, | |
middleware], | |
response_middleware=response, | |
async=True) | |
def server(callable=None, **params): | |
return wsgi.WSGIServer(Site(), **params) | |
if __name__ == '__main__': # pragma nocover | |
server().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment