# CGI-BIN Timeout Question with Python



## seanx820 (Mar 2, 2012)

Hello all,

I am trying to write a webpage wrapper for some scripts I have written in python on my FreeBSD 9 machine. 
When I do this on the command line

`# python tests.py`

it works fine, although the script takes 8 full seconds to run.. (normal) when I do a:


```
proc = subprocess.Popen(['/usr/local/bin/python', 'tests.py'], stdout=subprocess.PIPE)
```

in a cgi-bin .py webpage the script seems to time out.  There are no errors in the log (/var/log/httpd-error.log).  

When I make a â€˜fakeâ€™ shorter version of the script that only takes a fraction of a second to run (versus the full 8 second version) the subprocess.Popen command works fine and returns the data fine to the web browser.  Doing some digging it looks like the cgi-bin timeout is default at 300 seconds which I am nowhere near. I am not sure where to go at this point. I was thinking of running this in the background so the script can continue on using (&) and then checking afterwards for a results file or something, but that seems like a crappy fix.  Sorry to be so ambiguous but I have no errors (just behavior) to work with and I think itâ€™s probably an architecture problem with the way I am approaching this on apache.  I am open to any and all suggestions


----------



## SirDice (Mar 2, 2012)

Try adding some sort of logging to your script. Have it open a temporary file and write some information about what the script is doing.

I'm sorry I can't be more specific, I've never programmed in python. I do use perl and this 'simple' trick can be a real life-saver at times.


----------



## draco003 (Mar 2, 2012)

*H*ave you tried changing the subprocess command to work on bash instead of invoking python*?*

*T*ry this:


```
proc = subprocess.Popen(['/usr/local/bin/bash', '-c', 'tests.py'], stdout=subprocess.PIPE)
```

Enjoy =)


----------



## seanx820 (Mar 2, 2012)

@SirDice, have the same problem with perl doing this 
	
	



```
system("/usr/local/bin/python /webroot/tests.py ");
```

@Draco, going to try, but I am going to need to feed some sort of feedback to the web browser... let me see


----------



## seanx820 (Mar 2, 2012)

@Draco

That just errors out like this:


```
/usr/local/bin/bash: tests.py: command not found
```


----------



## seanx820 (Mar 2, 2012)

I got it working with this:


```
proc = subprocess.Popen(['/usr/local/bin/python', 'tests.py'], stdout=subprocess.PIPE)
out, err = proc.communicate()
print '<textarea>'
print out
print '</textarea>'
proc.stdout.close()
```

Except when my engine (the underlying code) has to do threading it won't work; not sure if apache22 can do threading or not; going to make a quick program that is very simple and see if it works.

Thanks all.


----------



## SirDice (Mar 3, 2012)

seanx820 said:
			
		

> @SirDice, have the same problem with perl doing this
> 
> 
> 
> ...


I didn't say you should use perl, I meant using a temporary file and logging the things your scripts do. Which is a trick I regularly use when programming in perl.


----------

