Just for instance, this code is working
import urllib
image = urllib.URLopener()
file_ = 1
name = 1
for i in range(1,1000):
try:
image.retrieve("http://mangawriter.com/pics/pic"+str(file_)+".jpeg","pic"+str(name)+".jpeg")
print "save file %s" %file_
name += 1
file_ += 1
except IOError:
file_ += 1
How could I make it stop after some time was spent, even if the code is still being ran? Please, help me to figure it out.
I would use the
multiprocessingmodule, which generates new processes (not threads) for parallelizing tasks.How to do it? First, the actual download code should be put in a function:
Now, we create a new
multiprocessing.Processobject, passing the function above as its target. This object will start a process just to execute this function:Once we have created the process object, just call its
start()method. This will start a process that will run in parallel:Since it is running in parallel, the main program keeps executing. Now, we define a timeout and sleep for the time of this timeout. In the example below, the timeout is 15 seconds:
Once the timeout ends, just terminate the downloader process:
The full program can be found here.