In A.py, I use os.system( "B.py %s" % searchPath ) to invoke B.py. B.py will do some logical operations and get some data.
How can I let B.py returns these data to A.py, what should I do in B.py?
In A.py, I use os.system( "B.py %s" % searchPath ) to invoke B.py. B.py will do some logical operations and get some data.
How can I let B.py returns these data to A.py, what should I do in B.py?
There are too many ways:
Update:
Queue example: check out https://docs.python.org/2/library/queue.html?highlight=queue
Example of how to wait for enqueued tasks to be completed from above link:
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
If you cannot directly invoke B from A you can consider the following:
Use stdout
and JSON. I have used this technique to integrate applications that are written in different languages. B.py
will do all the calculations, store them in a dict
or list
structure and when it is finished call json.dumps
. The advantage of this is that you can also do error/warning handling by returnring structures like {"status": "OK", "warning": "Could not find XYZ... skipped"}
Use an intermediate stage. This is preferred when B.py
returns a large amount of data. The medium can be a temporary file or a database. You can also combine both! and make B.py
to create a database in a file (sqlite3
)
If the data that you return are minimal, you can consider using exit status only to indicate success or failure...
Hope it helps
I think you are looking at subprocesses. The solution to your question would look something like
import subprocess
proc = subprocess.Popen(["B.py", searchpath], stdout=subprocess.PIPE)
(out, err) = proc.communicate()
print "program output:", out
For a similar question: Equivalent of Bash Backticks in Python
I think that the question is a little bit generic. You can use an IPC method to pass data between 2 processes (or 2 different runs of 2 scripts which is the same):
https://en.wikipedia.org/wiki/Inter-process_communication
Usually the more basic ones are piping stdout-stdin or using a file. For example the Popen object of the subprocess module has a communicate method which is useful to get the data:
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
output=`dmesg | grep hda`
# becomes
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]