J'essaie de créer un MainObject disponible en tant que service DBus. Ce MainObject doit toujours rester sensible aux autres objets/processus et pour ce non-blocage même lors du traitement de ses éléments. pour cette raison, les éléments sont traités les uns après les autres dans un fil séparé (style de file d'attente). Vous pouvez ajouter des éléments à MainObject via DBus ou CommandLine. J'ai simplifié l'échantillon (pas de dbus, pas de ligne de commande) pour montrer mon problème.L'application python multithread se bloque lors de l'exécution de ses threads
Mon problème est, que si je réactive 'tt.join()' l'application fonctionne comme prévu, mais elle bloque les autres processus. Pas étonnant, tt.join fait attendre l'application jusqu'à ce que le thread séparé a terminé son travail. D'un autre côté, si 'tt.join()' reste désactivé, l'application ne bloque pas les événements dbus externes, mais n'arrive jamais à 'ThreadTest done!' (regardez la sortie réelle)
Ce que je veux, c'est ma sortie attendue mais les applications devraient rester réactives.
#!/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()
résultat attendu:
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!
production réelle:
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 ...
Dupliate: http://stackoverflow.com/questions/1796588/python-thread-and-gobject – ebo