Multithreaded python application freezes when threads start

I am trying to create a MainObject that can be used as a DBus service. This MainObject should always react to other objects / processes for this non-blocking system even when processing its elements. for this reason, the elements are processed in a separate thread one after another (queue style). You can add items to MainObject through DBus or CommandLine. I simplified the sample (no dbus, no command line) to show my problem.

My problem is that if I close 'tt.join () again, the application works as expected, but it blocks other processes. Unsurprisingly, tt.join makes the application wait until a separate thread completes its work. On the other hand, if "tt.join ()" remains disabled, the application is not blocked for external dbus events, but never comes to "ThreadTest done!". (look at the real way out)

What I want is my expected result, but applications should remain responsive.

#!/usr/bin/python2.5

import gobject
import threading
import re
import time

class ThreadTest(threading.Thread):

  def __init__(self):
    threading.Thread.__init__ (self)    
    print '  ThreadTest created!'

  def run(self):
    print '  ThreadTest running ...'
    time.sleep(1)
    print '  ThreadTest done!'
    return True

class MainObject():
  def __init__(self):
    self.timer = gobject.timeout_add(1000, self.update)
    self.loop  = gobject.MainLoop()
    print 'MainObject created!'

  def update(self):
    print 'MainObject updating ...'
    if self.check_running() == False:
      tt = ThreadTest()
      tt.start()
      #tt.join()
    return True

  def check_running(self):
    running = False
    expr = re.compile('ThreadTest')
    for threadstr in threading.enumerate():
      matches = expr.findall(str(threadstr))
      if matches:
        running = True
    return running  


mo = MainObject()
mo.loop.run()

expected output:

MainObject created!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
  ThreadTest done!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
  ThreadTest done!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
  ThreadTest done!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
  ThreadTest done!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
  ThreadTest done!

real conclusion:

MainObject created!
MainObject updating ...
  ThreadTest created!
  ThreadTest running ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
MainObject updating ...
+3
source share
2 answers

gobject . gobject:

gobject.threads_init()
+2

python - . GIL - Python Global Interpreter.

, , "" ( python 2.6) - "threading", : jsut htreading - : dbus, .. "" - (.. ) - (, , ..)... .

, , Voodo threading.enumerate?? Python "in" ( str ):

check_running :

def check_running(self):
    return 'ThreadTest' in str(threading.enumerate())
0

Source: https://habr.com/ru/post/1727367/


All Articles