0

I have 2 independent python 2 applications running in the same linux (ubuntu) computer.

I want to send messages from one to another (bidirectional) and receives these messages inside a callback function.

Is it possible? Do you have any example as reference?

Thanks

Marco Arruda
  • 699
  • 6
  • 17

1 Answers1

0

There are different options available for communicating between python apps.

A simple one would be to use an API based on HTTP. Each application will expose an specific port and communication takes place by exchanging HTTP requests. There are several frameworks that allow you to build it in few steps. For example, using Bottle:

In app1:

from bottle import route, run, request

@route('/action_1', method='POST')
def action_1_handler():
    data = request.json
    print(str(data))
    # Do something with data
    return {'success': True, 'data': {'some_data': 1}}

run(host='localhost', port=8080)

In app2:

import requests
r = requests.post("http://localhost:8080/action_1", json={'v1': 123, 'v2': 'foo'})
print r.status_code
# 200
data = r.json()
# {u'data': {u'some_data': 1}, u'success': True}

Note that if the action executed at app1 after receiving the HTTP request takes lot of time, this could result in a timeout error. In such a case, consider to run the action in another thread or use an alternative communication protocol (e.g. sockets, ZeroMQ Messaging Library).

Some related reads:

fikipollo
  • 528
  • 5
  • 9
  • Thank you. I have used `ZeroRPC` (https://www.zerorpc.io/), which is based on `Zeromq` an provides Python, NodeJS and Shell interfaces. I wrote a post [here] (https://www.linkedin.com/feed/update/urn:li:activity:6495780259076538368) about my first impressions. – Marco Arruda Feb 28 '19 at 15:35