Bonjour,

j'ai un algorithme en Python 3.7 avec IDLE :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
36
37
38
39
40
41
42
43
44
from threading import Thread,RLock
from math import sqrt
 
verrou=RLock()
 
class testthread(Thread):
 
    def __init__(self,debut,fin):
        Thread.__init__(self)
        self.debut=debut
        self.fin=fin
 
    def run(self):
        a=1
        for i in range(self.debut,self.fin):
          l,d,n=1,2,i
          while n>1:
            if n%d==0:
              l=d
              n/=d
            else:
              d+=1
          if l<sqrt(i):
            with verrou:
              a+=1
        print(a)
 
# Création des threads
thread_1 = testthread(1,500)
thread_2 = testthread(500,10001)
#thread_3 = testthread(500,750)
#thread_4 = testthread(750,1001)
 
# Lancement des threads
thread_1.start()
thread_2.start()
#thread_3.start()
#thread_4.start()
 
# Attend que les threads se terminent
thread_1.join()
thread_2.join()
#thread_3.join()
#thread_4.join()
Peu importe ce qu'il calcule exactement, vous voyez qu'il s'agit d'une structure basique pour l'utilisation de thread.

Mon problème est que je croyais que thread permettait de faire de la programmation parallèle, c'est-à-dire faire fonctionner le même algorithme plusieurs fois mais en même temps.
Ici je crois qu'il devrait faire tourner for de 1 à 500 et de 500 à 1000 en même temps, mais ce n'est pas ce qui se passe : on dirait qu'il calcule d'abord thread_1 puis thread_2 !
Si quelqu'un pouvait m'aider à effectuer cette boucle plusieurs fois en même temps ce serait génial