Sto studiando il multi-threading perciò siate pazienti per favore: perchè il "prova3()" non acquista il lock e non aspetta 10 secondi prima di procedere ad eseguire prova2() e prova1() ?codice:# -*- coding: utf-8 -*- class Boh(object): def __init__(self, **kwargs): self.__lock = Lock() def main(self): t1 = Thread(target=self.prova1) t2 = Thread(target=self.prova2) t3 = Thread(target=self.prova3) t1.start() t2.start() t3.start() """t1.join() t2.join() t3.join()""" while True: print 4 sleep(1) def prova1(self): while True: print "1" sleep(1) def prova2(self): while True: print "2" sleep(1) def prova3(self): self.__lock.acquire() try: while True: print "3" sleep(10) finally: self.__lock.release() if __name__ == "__main__": from time import sleep, time from threading import Thread, Lock Boh().main()
// Edit
Ho anche modificato con:
Per acquisire il lock ogni start ciclo e rilasciarlo 5 secondi dopo la fine ma continuano ad eseguirsi quelli di prova2/1()codice:def prova3(self): while True: self.__lock.acquire() print "3" sleep(5) self.__lock.release()

Rispondi quotando