Search code examples
pythontornado

build mircoservice system by using Tornado Framework


Trying to build small mircoservice system by using Tornado framework. Here is the sturcture:

-users_service
-books_service
-public_api_service

so users_service and books_service would connect to their own database like users.db and books.db (for example : books_service is running on localhost:6000, and public_api_service is running on localhost:7000), and public_api would be opned to users, so when users call public api, public_api_service would send a request to users_servcice or books_service and get their response(json format), then format them and response. my question is how to properly send a request from public_api_service to users_service or books_service?

def get_listings_info(page_num, page_size):
        url_params = {
            # 'user_id': user_id,
            'page_num': page_num,
            'page_size': page_size
        }
        url = url_concat('http://127.0.0.1:6000/books', url_params)
        request = HTTPRequest(url=url, method='GET')
        # http_client = AsyncHTTPClient()
        http_client = HTTPClient()
        result = http_client.fetch(request)
        result = json.loads(result.body)
        # return result.body
        return result

I tired this method, but got a this error: RuntimeError: Cannot run the event loop while another loop is running. Any help would be apprecatied.


Solution

  • My guess is that you are trying to run this code from inside of a Tornado application and HTTPClient is meant to be standalone.

    From the Tornado documentation for HTTPClient:

    Applications that are running an IOLoop must use AsyncHTTPClient instead.

    This means that if you are running a Tornado application (which uses an IOLoop), the HTTPClient will not work and you should use the AsyncHTTPClient instead.

    See the documentation for Tornado web clients here: https://www.tornadoweb.org/en/stable/httpclient.html