17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Retrait du verrou<br />

[...]<br />

def _manip(self):<br />

for i in range(5):<br />

#locker.acquire()<br />

try:<br />

liste.remove('a')<br />

sleep(0.1)<br />

liste.insert(0, 'a')<br />

finally:<br />

pass<br />

#locker.release()<br />

[...]<br />

Bonnes pratiques et optimisation du code<br />

CHAPITRE 13<br />

$ python threaded.py<br />

Exception in thread Thread-2:<br />

Traceback (most recent call last):<br />

File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap<br />

self.run()<br />

File "threaded.py", line 28, in run<br />

self._manip()<br />

File "threaded.py", line 18, in _manip<br />

liste.remove('a')<br />

ValueError: list.remove(x): x notin list<br />

Exception in thread Thread-3:<br />

Traceback (most recent call last):<br />

File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap<br />

self.run()<br />

File "threaded.py", line 28, in run<br />

self._manip()<br />

File "threaded.py", line 18, in _manip<br />

liste.remove('a')<br />

[...]<br />

Outre les sections critiques, il existe un autre mécanisme qui permet de coordonner<br />

le travail de plusieurs threads : les événements définis par des objets de type Event.<br />

class Event()<br />

Renvoie une instance de type Event, qui peut être considérée comme un drapeau.<br />

Cette classe fournit des méthodes pour déterminer l’état du drapeau. Les threads<br />

peuvent manipuler ces objets pour se coordonner. L’état interne du drapeau est à<br />

False lorsque l’objet est instancié.<br />

459

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!