Package: vyked
Vyked is an asyncio based python framework for service oriented architectures.RequirementsPython >= 3.3asynciohttps://pypi.python.org/pypi/asyncioInstallation$pipinstallvykedVyked usesjsonstreamer, a fast streaming JSON parser for Python that
generates SAX-like events using the yajl C library. To install yajl:$gitclonegit@github.com:lloyd/yajl.git$cdyajl$./configure&&makeinstallGetting StartedService RegistryWhen Vyked services start they register with the service registry by advertising their name, ip,port and the names of other Vyked services they might require to function. The registry then co-ordinates all Vyked services and even balances loads across multiple instances of each service.Vyked uses redis for pub-sub and registry storage, you must start a redis instance before you start the vyked registry.You must start the Vyked registry before any services, as such:$python-mvyked.registryor :fromvykedimportRegistryregistry=Registry('127.0.0.1',4500)registry.start()ServiceVyked allows you to host HTTP and TCP services.TCP ServicesVyked TCP services provide an RPC-like api by decorating typical class methods with decorators. There are two types of api’s available,Request-Response using @api to provide such an api and @request to consume it from a remote clientPublish-Subscribe using @publish to automatically publish an event and @subscribe to receive it at a remote clientThe following basic examples illustrate these decorators for both Services and remote Clients.Sample TCP service:fromasyncioimportsleepfromvykedimportHost,TCPService,api,publishclassIdentityTCPService(TCPService):def__init__(self,ip,port):super(IdentityTCPService,self).__init__("IdentityService",1,ip,port)@apidefcreate(self,user_name,password):result=yield fromsleep(5)#long running taskifuser_nameisNone:raiseException('username cannot be none')returnresult@publishdefpassword_changed(self,user_name):""" calling this method from within your code will cause a 'password_changed' event to be published
        to all subscribing services
        """# @publish decorated methods must return a dict of values to be publishedreturnlocals()# easy way to return a dict containing all the params - in this case, user_name.Sample HTTP service:Vyked uses aiohttp to setup HTTP server.fromvykedimportHost,HTTPService,get,post,Response,RequestclassIdentityHTTPService(HTTPService):def__init__(self,ip,port):super(IdentityHTTPService,self).__init__("IdentityService",1,ip,port)@get(path='/users/{username}')defget(self,request:Request):username=request.match_info.get('username')returnResponse(status=200,body=("Hello{}".format(username)).encode())@post(path='/users/{username}')defcreate(self,request:Request):data=yield fromrequest.json()returnResponse(status=200,body=(json.dumps(data)).encode())To start a service:if__name__=='__main__':http=IdentityHTTPService('0.0.0.0',4501)tcp=IdentityTCPService('0.0.0.0',4502)Host.registry_host='127.0.0.1'Host.registry_port=4500Host.pubsub_host='127.0.0.1'Host.pubsub_port=6379Host.name='Identity'Host.attach_service(http)Host.attach_service(tcp)Host.run()ClientSo far, the examples have only covered standalone services. But a service might interact with other services.
To make such an interaction possible, vyked provides a TCP and HTTP client to interact with TCP and HTTP service respectively.Sample TCP client for IdentityService we saw in the above example:fromvykedimportHost,TCPService,TCPServiceClient,api,publish,request,subscribeimportasyncioclassIdentityClient(TCPServiceClient):def__init__(self):super(IdentityClient,self).__init__("IdentityService",1)@requestdefcreate(self,user_name,password):returnlocals()#@request requires a dict containing params describing the request payload@subscribedefpassword_changed(self,user_name):print("Password changed event received")yield fromasyncio.sleep(4)Sample HTTP Client:classHello(HTTPServiceClient):def__init__(self):super(Hello,self).__init__('Hello',1)@get()defperson(self,name):path='/{}'.format(name)params={'key':'value'}headers={'Content-Type':'application/json'}app_name='test'returnlocals()DocumentationView documentation atread-the-docsLicensevykedis offered under the MIT license.Source codeThe latest developer version is available in a github repository:https://github.com/kashifrazzaqui/vyked