mercredi 30 septembre 2015

Python. unclear behavior of IF-statement

I tried to solve the problem described in this question.

Briefly, I have 3 threads and one singleton class Data. All threads read and write values in Data. All writes concluded in threading.Lock():

locker.acquire()
# some writes
locker.release()

And I saw that if statement never ends sometimes.

if len(app_data.ip_table[app_data.cfg.MY_IP]['tasks']):  # Если есть задания для выполнения
        print u"Есть что выполнять"
        if (app_data.cfg.MULTITHREADING or app_data.complete_task.is_set()):  # Если текущее задание выполнено или включена мультипоточность
            job = Worker(locker, app_data, SRV.taskResultSendToSlaves, app_data.ip_table[app_data.cfg.MY_IP]['tasks'].pop())  # ipTable[MY_IP][0].pop()
            job.setDaemon(True)
            print 0
            job.start()  # Запуск в классе Worker на исполнение одного задания
        else:
            print u"Не выполнено"
        print u"Итерация 3"
    ##### THIS CODE ARE NEVER EXECUTE SOMETHIMES ######
        print 7.0
    print 7.1
    print 7.2
    locker.release()  # app_data.locker.release()
    print "unlock 6"
    ###################################################

All threads works fine, but in some random moment this code going to fall down. And highlighted code does not get executed, so locker.release() is never executed, so threads are in deadlock.

But why this IF statement falls down in some unclear times? Any ideas?

Aucun commentaire:

Enregistrer un commentaire