How to set a timeout?

Marco Mariani m.mariani at imola.nettuno.it
Wed Jul 18 09:40:19 EDT 2001


On Wed, Jul 18, 2001 at 03:51:51PM +0200, Matthias Huening wrote:

> I have a function like this:
> 
> -----
> def read_pages(urllist):
>     res = {}
>     for x in urllist:
>         try:
>             res[x] = urllib.urlopen(x).read()
>         except:
>             pass
>     return(res)
> -----
> 
> I now want the following behaviour: try to catch the webpage for 3 seconds;
> if it takes longer just skip this one and move on to the next. How can I
> achieve this?


>From one of my sources:



from signal import *


def timeout(signum, frame):
	raise IOError, "Timeout!"

	def alarm_on(self):
		signal(SIGALRM, timeout)
		alarm(1)

	def alarm_off(self):
		alarm(0)




[...]
	self.alarm_on()
	try:
		self.fd = os.open('/dev/'+self.devname, os.O_RDWR | os.O_NOCTTY)
	except:
		self.fd = -1
	self.alarm_off()
[...]






More information about the Python-list mailing list