简体   繁体   中英

Run external program concurrently in Python

I'm wondering how to call an external program in such a way that allows the user to continue to interact with my program's UI (built using tkinter, if it matters) while the Python program is running. The program waits for the user to select files to copy, so they should still be able to select and copy files while the external program is running. The external program is Adobe Flash Player.

Perhaps some of the difficulty is due to the fact that I have a threaded "worker" class? It updates the progress bar while it does the copying. I would like the progress bars to update even if the Flash Player is open.

  1. I tried the subprocess module. The program runs, however it prevents the user from using the UI until the Flash Player is closed. Also, the copying still seems to occur in the background, it's just that the progress bar does not update until the Flash Player is closed.

     def run_clip(): flash_filepath = "C:\\\\path\\\\to\\\\file.exe" # halts UI until flash player is closed... subprocess.call([flash_filepath]) 
  2. Next, I tried using the concurrent.futures module (I was using Python 3 anyway). Since I'm still using subprocess to call the application, it's not surprising that this code behaves exactly like the above example.

     def run_clip(): with futures.ProcessPoolExecutor() as executor: flash_filepath = "C:\\\\path\\\\to\\\\file.exe" executor.submit(subprocess.call(animate_filepath)) 

Does the problem lie in using subprocess ? If so, is there a better way to call the external program? Thanks in advance.

You just need to keep reading about the subprocess module, specifically about Popen .

To run a background process concurrently, you need to use subprocess.Popen :

import subprocess

child = subprocess.Popen([flash_filepath])
# At this point, the child process runs concurrently with the current process

# Do other stuff

# And later on, when you need the subprocess to finish or whatever
result = child.wait()

You can also interact with the subprocess' input and output streams via members of the Popen -object (in this case child ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM