Nodari L
Nodari L

Reputation: 1178

Asynchronous request to a process with python

I have a question about asynchronous call to a running process. For example, I have a program written in python. (call it test.py)

import time

def run():
   while True:
       print "This is run function print"
       time.spleep(2)

def get_dict()
   return {'a': 1}

if __name__ == "__main__":
    run()

Now, I run test.py, and after that I want asynchronously make a web request to that process and get the value of a get_dict function. Is it possible?

I ave tried to use tornado IOLoop, to make a web request to the test.py

I am absolute zero in async programming, and really have no a clear idea, how it should be done. Because of that, my question can be very confusing.

Upvotes: 0

Views: 278

Answers (2)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

Install Tornado and run this:

from datetime import timedelta
from tornado import gen, ioloop, web

i = 0

@gen.coroutine
def run():
    global i
    loop = ioloop.IOLoop.current()
    while True:
        print "This is run function print"
        yield gen.Task(loop.add_timeout, timedelta(seconds=1))
        i += 1


def get_dict():
    return {'a': i}


class DictHandler(web.RequestHandler):
    def get(self):
        # Tornado converts dicts to JSON.
        self.finish(get_dict())

if __name__ == "__main__":
    application = web.Application([
        ('/dict', DictHandler),
    ])
    application.listen(8888)
    print 'Listening on http://localhost:8888'
    ioloop.IOLoop.current().run_sync(run)

IOLoop.run_sync runs until your run method quits. Visit http://localhost:8888 to see the current value of i. Hit Ctrl-C in the terminal to end the program.

Upvotes: 1

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95629

It sounds like you want your Python application to include a server that responds to external requests and can execute functions on behalf of these external requests. Some ways of doing this include embedding a full-fledged HTTP server in your Python application to expose this kind of functionality or embedding a lighter-weight RPC server. Since it sounds like you want to expose this functionality to another application, an RPC approach is probably the more sensible one.

Personally, I would recommend using a combination of protobuf and protobuf-socket-rpc. The protocol buffers library makes it easy to declare the data and RPC service functionality in an interface definition language (protocol buffers) and generate code in Python, C++, Java, and other languages that can read/write data in this format. The protocol buffer code also generates a generic service stub that you can use to provide an implementation of a service. The protobuf-socket-rpc library allows you to easily create a server that exports your service implementation as well as to create a remote/rpc client that executes by making a connection to the application that provides the service implementation.

Upvotes: 0

Related Questions