python - Error When Testing threading Module -


i looking implement threading module (python3), , wanted test out first see how reduce runtime. following code not implement multithreading:

import threading, time import queue q = queue.queue(10000000)  def fill_list():     global q     while true:         q.put(1)         if q.qsize() == 10000000:             return  t1 = time.clock() fill_list() tend = time.clock() - t1  print(tend) 

this produces:

>>> 29.939367999999998 >>> 

then tried complete same task 2 threads in effort decrease run time.

import threading, time import queue  q = queue.queue(10000000)  def fill_list():     global q      while true:         q.put(1)         if q.qsize() == 10000000:             return  t1 = time.clock() thread1 = threading.thread(target=fill_list, args=()) thread2 = threading.thread(target=fill_list, args=()) thread1.start() thread2.start() tend = time.clock() - t1 print(q.qsize()) print(tend) 

this produces:

>>>  13722 0.018415999999999988 >>>  

so completed faster, did not complete task of filling queue 10000000. don't understand why method return if conditional has not been met.

am implementing threading module incorrectly? problem 2 threads trying access same queue? thank you

you need wait threads finish, main continues execution while 2 threads still running.

add fix it.

thread1.start() thread2.start() thread1.join() thread2.join() tend = time.clock() - t1 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -