Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyboardInterrupt does not work in multi threading python
    primarykey
    data
    text
    <p>I am trying to do multi threading to check the network connection. My code is:</p> <pre><code>exitFlag = 0 lst_doxygen=[] lst_sphinx=[] class myThread (threading.Thread): def __init__(self, counter): threading.Thread.__init__(self) self.counter=counter def run(self): print "Starting thread" link_urls(self.counter) def link_urls(delay): global lst_doxygen global lst_sphinx global exitFlag while exitFlag==0: try: if network_connection() is True: try: links = lxml.html.parse(gr.prefs().get_string('grc', 'doxygen_base_uri', '').split(',')[1]+"annotated.html").xpath("//a/@href") for url in links: lst_doxygen.append(url) links = lxml.html.parse(gr.prefs().get_string('grc', 'sphinx_base_uri', '').split(',')[1]+"genindex.html").xpath("//a/@href") for url in links: lst_sphinx.append(url) exitFlag=1 except IOError, AttributeError: pass time.sleep(delay) print "my" except KeyboardInterrupt: exitFlag=1 def network_connection(): network=False try: response = urllib2.urlopen("http://google.com", None, 2.5) network=True except urllib2.URLError, e: pass return network </code></pre> <p>I have set a flag to stop the thread inside while loop. I also want to exit the thread by pressing Ctrl-C. So I have used try-except but thread is still working and does not exit. If I try to use </p> <pre><code>if KeyboardInterrupt: exitFlag=1 </code></pre> <p>instead of try-except, thread just works for first time execution of while loop and then exist. p.s. I have created the instance of myThread class in another module.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload