thread_count=2 def worker(th): func.putlog("--стартовал поток "+str(th)); i=0 while i<thread_count: func.putlog("--поток "+str(th)+", шаг "+str(i)) time.sleep(1) i=i+1 def main(): func.putlog("-скрипт стартовал!"); #запускаем потоки for _ in xrange(thread_count): thread_ = threading.Thread(target=worker(_)) thread_.start() #ждем пока потоки завершатся while threading.active_count() >1: time.sleep(1) func.putlog("-скрипт закончил работу!"); main(); 

Displays consecutively:

  -скрипт стартовал! --стартовал поток 0 --поток 0, шаг 0 --поток 0, шаг 1 --стартовал поток 1 --поток 1, шаг 0 --поток 1, шаг 1 -скрипт закончил работу! 

Although in theory should all be executed in parallel?

  • Correct indents in cycles. - mkkik

1 answer 1

You call the worker function when initializing the thread, in the thread_ = threading.Thread(target=worker(_)) line. thread_ = threading.Thread(target=worker(_)) and pass its output to the thread constructor - None. I.e. threads don't run at all

  • thread_ = threading.Thread(target=worker, args=(_,)) - mkkik
  • Pasib, earned - Mushrooms Paul