Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between two python scripts

a methodology question:

I have a "main" python script which runs on an infinite loop on my system, and I want to send information to it (a json data string for example) occasionally with some other python scripts that will be started later by myself or another program and will end just after sending the string.

I can't use subprocess here because my main script doesn't know when the other will run and what code they will execute.

I'm thinking of making the main script listen on a local port and making the other scripts send it the strings on that port, but is there a better way to do it ?

like image 697
Dettorer Avatar asked Apr 25 '13 11:04

Dettorer


People also ask

How do I pass data between two Python scripts?

you can use multiprocessing module to implement a Pipe between the two modules. Then you can start one of the modules as a Process and use the Pipe to communicate with it. The best part about using pipes is you can also pass python objects like dict,list through it.

Can you call one Python script from another?

The first line of 'import python_2' in the python_1 script, would call the second python_2 script. Whenever you want to run one Python script from another, you'll need to import the exact name of the Python script that you'd like to call.

Can I run 2 Python scripts at the same time?

You can create a fourth python file d.py in the same folder as other 3 python files, which imports the other 3 python files and runs their functions, as shown below. In this article, we have learnt how to run multiple python files.


3 Answers

zeromq: http://www.zeromq.org/ - is best solution for interprocess communications imho and have a excelent binding for python: http://www.zeromq.org/bindings:python

like image 139
Vladimir Muzhilov Avatar answered Sep 27 '22 01:09

Vladimir Muzhilov


Since the "main" script looks like a service you can enhance it with a web API. bottle is the perfect solution for this. With this additional code your python script is able to receive requests and process them:

import json

from bottle import run, post, request, response

@post('/process')
def my_process():
  req_obj = json.loads(request.body.read())
  # do something with req_obj
  # ...
  return 'All done'

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

The client script may use the httplib to send a message to the server and read the response:

import httplib

c = httplib.HTTPConnection('localhost', 8080)
c.request('POST', '/process', '{}')
doc = c.getresponse().read()
print doc
# 'All done'
like image 37
mike Avatar answered Sep 23 '22 01:09

mike


In case you are interested in implementing the client script that Mike presented in Python 3.x, you will quickly find that there is no httplib available. Fortunately, the same thing is done with the library http.client.

Otherwise it is the same:

import http.client
c = http.client.HTTPConnection('localhost', 8080)
c.request('POST', '/process', '{}')
doc = c.getresponse().read()
print(doc)

Though this is old I would figure I would post this since I had a similar question today but using a server.

like image 21
Bradley Robinson Avatar answered Sep 26 '22 01:09

Bradley Robinson