Lancer plusieurs thread en python
djamel
-
sebsauvage Messages postés 32893 Date d'inscription Statut Modérateur Dernière intervention -
sebsauvage Messages postés 32893 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour;
J'essaye de lancer plusieurs thred ds une boucle, mais il ne se passe rien ?
Le code :
# script run
import sys, glob, socket, thread, os, os.path, string
from ftplib import FTP
dir_result = '../RES'
EXTFIC = ".res"
dir_tmp = '../tmp'
def init(script_name):
dir_part, file_part = os.path.split(script_name)
fichier= dir_result+'/'+file_part[:-3]+EXTFIC
if not os.path.exists(dir_result):
os.mkdir(dir_result)
else:
if os.path.isfile(fichier):
os.system('del "%s"' % fichier)
return fichier
def Telecharger(dir):
listefile = []
ftp = FTP('stldev')
ftp.connect('stldev', '21')
ftp.login('proftpd', 'password')
remote_dir = string.replace(os.path.join('terminaux', 'M16-0600-2704' ), os.sep, '/' )
ftp.cwd(remote_dir)
ftp.pwd()
ftp.dir()
listefile = ftp.nlst(".")
for file in listefile:
file_cible = open(os.path.join(os.path.dirname(dir), file), 'w+')
ftp.retrbinary("RETR "+ file, file_cible.write,1024)
file_cible.close()
fichier = init(sys.argv[0])
fsock1 = open(fichier , 'w+')
fsock2 = open(fichier, 'a+')
sys.stdout = fsock1
sys.stderr = fsock2
for i in range (10):
dir_tmp = '../tmp' + `i`
if not os.path.exists(dir_tmp):
os.mkdir(dir_tmp)
thread.start_new_thread(Telecharger, (dir_tmp,))
saveout = sys.stdout
fsock1.close()
fsock2.close()
A+;
J'essaye de lancer plusieurs thred ds une boucle, mais il ne se passe rien ?
Le code :
# script run
import sys, glob, socket, thread, os, os.path, string
from ftplib import FTP
dir_result = '../RES'
EXTFIC = ".res"
dir_tmp = '../tmp'
def init(script_name):
dir_part, file_part = os.path.split(script_name)
fichier= dir_result+'/'+file_part[:-3]+EXTFIC
if not os.path.exists(dir_result):
os.mkdir(dir_result)
else:
if os.path.isfile(fichier):
os.system('del "%s"' % fichier)
return fichier
def Telecharger(dir):
listefile = []
ftp = FTP('stldev')
ftp.connect('stldev', '21')
ftp.login('proftpd', 'password')
remote_dir = string.replace(os.path.join('terminaux', 'M16-0600-2704' ), os.sep, '/' )
ftp.cwd(remote_dir)
ftp.pwd()
ftp.dir()
listefile = ftp.nlst(".")
for file in listefile:
file_cible = open(os.path.join(os.path.dirname(dir), file), 'w+')
ftp.retrbinary("RETR "+ file, file_cible.write,1024)
file_cible.close()
fichier = init(sys.argv[0])
fsock1 = open(fichier , 'w+')
fsock2 = open(fichier, 'a+')
sys.stdout = fsock1
sys.stderr = fsock2
for i in range (10):
dir_tmp = '../tmp' + `i`
if not os.path.exists(dir_tmp):
os.mkdir(dir_tmp)
thread.start_new_thread(Telecharger, (dir_tmp,))
saveout = sys.stdout
fsock1.close()
fsock2.close()
A+;
A voir également:
- Threads en python
- Citizen code python avis - Accueil - Outils
- Mot secret python pix ✓ - Forum Python
- \R python ✓ - Forum Python
- Afficher un message sur python "" ✓ - Forum Python
- Python est introuvable. exúcutez sans argument pour procúder ó l ✓ - Forum Python
2 réponses
Généralement, il est préférable de mettre les traitements dans une classe qui hérite de threading.Thread.
Voici un petit exemple de threads
(lance le programme suivant, ça va créer le fichier thr.py):
import zlib,base64
open('the.py','w+b').write(zlib.decompress(base64.decodestring("""
eNqVkE9LxDAQxe/5FONhaQsluIiXBQ+iF0FvexMpYTvtjjSTkkwPfnsTG1pbvDiX/HmT33sZsqPz
AkIWa/CGW2drkKtH0xL3irK8XKjLYEIA+zVflYuizz+76qQgVosdNA0xSdOUAYeuBp5sFlPt3+lN
d7X0pZOOT+EhARa2n3huXImd80BAnH7RY3n3S0o1emKB4uVC2RsOAW4KOCwWm/Y0EB0GxLHMY9Fp
mdnH+r5aI6qZ90pBYsr3D6Wy15shPsFTFIUcx9whWwetdaF2gY+3OXGiLPOl2We10GYckePgq53P
M1rjvenxTyNJRislO+kgxku5Rz2KIEsCwWCgI/4P8tMRl3PqLfXspgDDCoLgoijoLTGGCI31DZCY
yMI=""")))
Tu trouvera également un autre exemple de thread là:
http://wikipython.flibuste.net/moin.py/CodesDivers
Il y a également une autre possibilité que le multitheading, c'est l'utilisation de sockets ansynchrones (voir module asyncore).
ça permet de créer des application mono-threads très performantes en gestion des entrées/sorties réseau (comme medusa ou twisted matrix).
Voici un petit exemple de threads
(lance le programme suivant, ça va créer le fichier thr.py):
import zlib,base64
open('the.py','w+b').write(zlib.decompress(base64.decodestring("""
eNqVkE9LxDAQxe/5FONhaQsluIiXBQ+iF0FvexMpYTvtjjSTkkwPfnsTG1pbvDiX/HmT33sZsqPz
AkIWa/CGW2drkKtH0xL3irK8XKjLYEIA+zVflYuizz+76qQgVosdNA0xSdOUAYeuBp5sFlPt3+lN
d7X0pZOOT+EhARa2n3huXImd80BAnH7RY3n3S0o1emKB4uVC2RsOAW4KOCwWm/Y0EB0GxLHMY9Fp
mdnH+r5aI6qZ90pBYsr3D6Wy15shPsFTFIUcx9whWwetdaF2gY+3OXGiLPOl2We10GYckePgq53P
M1rjvenxTyNJRislO+kgxku5Rz2KIEsCwWCgI/4P8tMRl3PqLfXspgDDCoLgoijoLTGGCI31DZCY
yMI=""")))
Tu trouvera également un autre exemple de thread là:
http://wikipython.flibuste.net/moin.py/CodesDivers
Il y a également une autre possibilité que le multitheading, c'est l'utilisation de sockets ansynchrones (voir module asyncore).
ça permet de créer des application mono-threads très performantes en gestion des entrées/sorties réseau (comme medusa ou twisted matrix).
Si tu as besoin d'échanger des données entre le thread principal et les autres threads, pense à utiliser l'objet Queue qui est totalement thread-safe.
( Un exemple de communication par Queue entre threads:
http://starship.python.net/crew/aahz/IPC9/index.html )
J'ai utilisé cette méthode pour échanger des commandes et des données entre le thread principal et les threads dans gossyp:
http://sebsauvage.net/python/gossyp/
( Un exemple de communication par Queue entre threads:
http://starship.python.net/crew/aahz/IPC9/index.html )
J'ai utilisé cette méthode pour échanger des commandes et des données entre le thread principal et les threads dans gossyp:
http://sebsauvage.net/python/gossyp/