Threading vs input() : le retour
Bonjour,
étant confronté au problème décrit dans ce sujet, fermé d'où le pourquoi j'ouvre le mien:
https://www.developpez.net/forums/d1...ding-vs-input/
à savoir le bloquage par input() des threads lancés en parallèle, j'ai testé le programme proposé pour prouver que input() ne bloque pas:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from threading import Thread, Event
from time import sleep
done = Event()
def do_something():
count = 0
while not done.is_set():
print ('waiting', count)
count += 1
sleep(0.1)
p = Thread(target=do_something)
p.start()
while True:
try:
s = input('$$$ ')
except KeyboardInterrupt:
done.set()
break
print ('********', s)
p.join()
print ('Bye') |
et là... ça me prouve qu'il bloque.
Voici ce que j'obtiens:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $$$ waitinga
********0
a
$$$ z
waiting******** 1z
$$$ e
waiting******** 2e
$$$ r
waiting******** 3r
$$$ t
waiting******** 4t
$$$ y
waiting******** 5y
$$$
waiting 6
Bye
>>> |
Donc input() bloque bel et bien le threading avec Python 3.7.0 sous Windows 10.
la preuve avec le code modifié en ajoutant "sleep(1)" juste après "print ('********', s)":
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| waiting$$$ a
0********
a
waiting 1
waiting 2
waiting 3
waiting 4
waiting 5
waiting 6
waiting 7
waiting$$$ z
8********
z
waiting 9
waiting 10
waiting 11
waiting 12
waiting 13
waiting 14
waiting 15
$$$ e
waiting******** 16e
waiting 17
waiting 18
waiting 19
waiting 20
waiting 21
waiting 22
waiting 23
waiting 24
$$$
waiting 25
Bye
>>> |
Quelqu'un a-t'il une autre solution à me proposer?