0

I am using the subprocess.Popen function to run a command line. Without having to use Popen.wait(), I want to check the subprocess after it has finished using Popen.poll(). Any suggestions on how to do this?

import subprocess
job = subprocess.Popen('command line', shell = True)
print(job.poll())

As it is, I get job.poll() printed before the subprocess starts. I want it to wait until it ends. I don't want to use wait because the rest of the user interface becomes unusable until the process ends. This is in PyQt4.

theCHemist90
  • 47
  • 1
  • 8
  • Can you please clarify - *the rest of the user interface becomes unusable until the process ends.* ? – Anand S Kumar Aug 05 '15 at 19:26
  • Yes. I want my program to be able to run this command line in the background and let the user know when it has finished. However, I wanna be able to keep working on the program while the subprocess is going on. Using the wait function "blocks/freezes" the program and I cannot keep working on it until the subprocess ends. I hope this makes sense. – theCHemist90 Aug 05 '15 at 19:30
  • I think you are looking for interreupts – The Brofessor Aug 05 '15 at 19:44
  • possible duplicate of [Background thread with QThread in PyQt](http://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt) – ivan_pozdeev Aug 05 '15 at 20:06

1 Answers1

0

As Python - wait on a condition without high cpu usage says, there are only two ways in existence to wait for something: polling or setting up/using a notification system.

If it's UI - didn't you forget about one notification system you always have - the message queue?

Besides, you can always (and, if it's UI, should always) perform any time-consuming tasks in worker threads. If which case, you are just fine with a synchronous call.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • I am not familiar with Python well and I am learning as I work on making my program. I would really appreciate it if you could explain what you mean. Thanks. – theCHemist90 Aug 05 '15 at 19:36
  • For UI, a worker thread is actually the standard solution. You didn't tell anything about which UI library/related techs you use, so I cannot be any more specific as of now. – ivan_pozdeev Aug 05 '15 at 19:39
  • I am using PyQt4 to build the UI – theCHemist90 Aug 05 '15 at 19:42