multithreading - Threads are not joined properly in Idle -


i trying create work , thread pool. each thread pool tries pick work untill work empty. threads join. when run program in idle, seems threads not joining (sometimes executes properly). threads not participate in picking work properly.

edit:but when run same in command shell, works fine.

below related code.

import threading import time  threads = [] work = range(1000) lock = threading.lock()  def f():     global work     th = threading.currentthread()     name = str(th.getname())     while true:         lock.acquire()         try:             size = str(len(work)) ##            print "\n inside  "+ name +" "+ size + "\n"             if size!="0":                 w = work.pop()             else:                 break         finally:             lock.release()         print "\n"+ name +" "+ size + "\n"    start1 = time.time() in range(10):     t = threading.thread(target = f)     threads.append(t)     t.start()  th in threads:     print "joining"     th.join()  end1 = time.time()  start2 = time.time()  work = range(1000) in work:     print  end2 = time.time()  print end1-start1, end2-start2 

how know each thread not acquiring work pool? or threads not joining.

what guess case threads fast runners , drain pool before of slow runners can chance draw work pool , appears didn't friendly scheduler. able run through several hundred iterations , appears threads joined every time. tested modifying using python 2.7.8:

i=0 th in threads:     th.join() #blocks until successful     i+=1 assert == 10 

further more increase or decreasing thread count, pool size or number of iterations produces same results. unless there more code you've cut out code correct far can see.

edit: concerning idle - can tell idle has issues plenty when running threaded applications. this thread makes mention of them cannot find specific bugs or references why case. it's race condition in idle causing threads not run/look aren't running/etc.

also able execute code (with modification) in idle on system, assert passed , time taken printed without problem. if problem related you're seeing printed on console in idle important consider idle's print function not thread-safe. may not see output on every execution because idle flushing buffer , not writing (or writing on before printing).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -