1

Redhat linux

I am spawning a child process which is a shell script that updates system time. I have a timeout of 30 minutes in expect like below. My child process updates system time to 4 hours ahead along with other commands. When I execute these below lines I get a result of 1.

child = pexpect.spawn('/bin/bash',['-c',options.cmd])

result = child.expect([pexpect.EOF,pexpect.TIMEOUT],timeout=cmd_timeout)

How can I make pexpect to use a true timer to timeout?

yalkris
  • 2,596
  • 5
  • 31
  • 51

1 Answers1

1

pexpect uses time.time() that is affected by a system clock change. A better alternative might be time.monotonic() from Python 3.3 (see its analog on older versions). It is used by subprocess module if available.

You could implement timeout using a while loop with your time() function, using signal.alarm(), etc.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Now I am using subprocess.Popen with a while loop with sleep(1) for timeout. Main problem using subprocess module is that it needs the child process output to be piped and OS pipe has a limit of 65KB. To fix that I used tempfile as a buffer and I am reading from it. Please let me know if that's sufficient for running a child process with timeout where child process in/out's large data. – yalkris Oct 19 '12 at 16:05
  • by the way I am using python 2.4.3 and subprocess in that version doesn't have this timeout feature as in python 3.3. – yalkris Oct 19 '12 at 16:10
  • OS pipe buffer size is a problem only due to possibility of a dead-lock if subprocess' in/out are interleaved. If they are not; you could use [various ways to get output from a subprocess](http://stackoverflow.com/a/12606327/4279). If they are; pexpect is a simpler solution for a dialog-based interaction (you don't need to use select() or threads explicitly with it). From your description in the comment it is not clear what are trying to do. You could [ask a new question and provide more details](http://stackoverflow.com/questions/ask). – jfs Oct 19 '12 at 23:10