0

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?

xpy
  • 5,481
  • 3
  • 29
  • 48

4 Answers4

0

There are too many ways:

  1. write and read file;
  2. use Queue;
  3. as hiro protagonist says, import B into A and call B's functions.

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
Wingzero
  • 9,644
  • 10
  • 39
  • 80
0

If you cannot directly invoke B from A you can consider the following:

  1. 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"}

  2. 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)

  3. If the data that you return are minimal, you can consider using exit status only to indicate success or failure...

Hope it helps

urban
  • 5,392
  • 3
  • 19
  • 45
0

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

Community
  • 1
  • 1
Pholochtairze
  • 1,836
  • 1
  • 14
  • 18
  • I am not so clear, When I invoke B.py it will follow 2 parameters looks like 'B.py -f searchPath' and '-f' is permanent and searchPath is flexible. Is this method also possible for my question? Thank you – Kaiming Chen Feb 26 '16 at 09:19
0

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]
J_Zar
  • 2,034
  • 2
  • 21
  • 34