Improving the web page download code.

MRAB python at mrabarnett.plus.com
Wed Aug 28 11:12:40 EDT 2013


On 28/08/2013 07:23, mukesh tiwari wrote:
[snip]
> Initially I blocked the main using raw_input('') and it was working fine.
>
> u = Downloader()
> signal.signal( signal.SIGINT , u.handleexception)
> thread.start_new_thread ( u.createurl , () )
> for i in xrange ( 5 ) :
>              thread.start_new_thread ( u.downloadurl , () )
> #This is for blocking main
> raw_input('')
> When I pressed  ctrl-c then it's responding fine but now after switching to threading module, I am not able to kill my program using SIGINT ( ctrl-c ). Any idea how to signal SIGINT to threads ?
>
Try making them daemon threads. A daemon thread is one that will be 
killed when the main thread terminates.

> Now the changed code and I have to catch the SIGINT.
>          u = Downloader()
>          signal.signal( signal.SIGINT , u.handleexception)
>          urlcreator = threading.Thread ( target = u.createurl )
>
>          workers = []
>          for i in xrange ( 5 ):
>                  workers.append ( threading.Thread( target = u.downloadurl ) )
>
            urlcreator.daemon = True
>          urlcreator.start()

>          for w in workers:
            urlcreator.daemon = True
                    w.daemon = True
>                  w.start()
>
>          urlcreator.join()
>          for w in workers:
>                  w.join()
>




More information about the Python-list mailing list