0

I'm trying to use subprocess32 to timeout a program like so

process = subprocess32.Popen(program, stdin =subprocess32.PIPE,
                                      stdout=subprocess32.PIPE,
                                      stderr=subprocess32.PIPE)
out, err = process.communicate(input_file, timeout=10)

This raises a TypeError

TypeError: __init__() takes at least 3 arguments (2 given)

Without the timeout argument, everything works fine. But I installed subprocess32 specifically for that purpose following this post, Using module 'subprocess' with timeout.


Traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
     92                                            stdout=subprocess32.PIPE,
     93                                            stderr=subprocess32.PIPE)
---> 94         out, err = process.communicate(input_file, timeout=10)
     95
     96         if "Focus conditions specified by user have been achieved" in out:

C:\Anaconda\lib\site-packages\subprocess32.pyc in communicate(self, input, timeout)
    925
    926         try:
--> 927             stdout, stderr = self._communicate(input, endtime, timeout)
    928         finally:
    929             self._communication_started = True

C:\Anaconda\lib\site-packages\subprocess32.pyc in _communicate(self, input, endtime, orig_timeout)
   1188                 self.stdout_thread.join(self._remaining_time(endtime))
   1189                 if self.stdout_thread.isAlive():
-> 1190                     raise TimeoutExpired(self.args)
   1191             if self.stderr is not None:
   1192                 self.stderr_thread.join(self._remaining_time(endtime))

TypeError: __init__() takes at least 3 arguments (2 given)

My first thought was that I need to specify an endtime to communicate, since it says it needs at least 3 arguments, but after reading the docs I saw that

Deprecated since version 3.4: Do not use the endtime parameter. It is was unintentionally exposed in 3.3 but was left undocumented as it was intended to be private for internal use. Use timeout instead.

So now I don't know what the problem is.

Using Python 2.7.11 and subprocess32 3.2.7

Community
  • 1
  • 1
lanery
  • 5,222
  • 3
  • 29
  • 43
  • That looks like a bug in subprocess32. BTW, what are you trying to accomplish with the timeout? Are you aware of the fact that it does not kill the process after the timeout? You could achieve the same by calling `process.poll()` until timeout expires. Without subprocess32. – zvone Oct 05 '16 at 23:49
  • @zvone Thanks for your comment. I have since read http://stackoverflow.com/questions/36952245/subprocess-timeout-failure, so I am semi-aware of that now, but have not found a good workaround yet. All I want to do is call a program, store the output, and have it timeout after `t` seconds. – lanery Oct 06 '16 at 00:05
  • The thing with that is, it is usually not done. It is sort-of like turning you computer off by unplugging it. If you really want to, you can `time.sleep(timeout)`, followed by `if process.poll() is None: process.kill()` (instead of `communicate()`). Or `communicate()` in a thread and kill from the outside... In any case, you risk killing the process in the middle of something sensitive. – zvone Oct 06 '16 at 00:10
  • For future readers of this question, I found out that subprocess32 doesn't really work in windows and ended up switching to python 3. – lanery Oct 13 '16 at 03:21

0 Answers0