Python Thread

Da Andreabont's Wiki.

Il Python è possibile lanciare dei thread tramite l'uso della libreria threading

Esempio base

#!/usr/bin/python

import threading
import time

def mio_thread(delay, counter):
    while counter:
        time.sleep(delay)
        print("%s: %s" % (threading.currentThread().getName(), time.ctime(time.time())))
        counter -= 1

# Configuro esecuzione dei thread
thread1 = threading.Thread(name="Thread 1", target=mio_thread, args=(2, 3))
thread2 = threading.Thread(name="Thread 2", target=mio_thread, args=(1, 4))

# Eseguo i thread
thread1.start()
thread2.start()

# Attendo fine dei thread
thread1.join()
thread2.join()