Ciasto piekarz
Ciasto piekarz

Reputation: 8277

starting multiple threads using python?

I am trying to gulp threading, and started with Python Module of the week examples:

according to below code

import threading

def worker(arg=None):
    """thread worker function"""
    print 'Worker thread: %s\n' % arg
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=str(i), name="threadingPrac")
    threads.append(t)
    t.start()

does this mean that I am starting 5 threads ?

I have just started with threading so want to understand it better.

Upvotes: 0

Views: 66

Answers (2)

Deelaka
Deelaka

Reputation: 13721

Yes you can check the length of the list threads by adding this line at the bottom of your code:

print len(threads)

Output:

5 #Number of threads

Upvotes: 0

Christian Berendt
Christian Berendt

Reputation: 3556

Yes.

Add import time and time.sleep(5) after the print statement to better see it.

import threading
import time

def worker(arg=None):
    """thread worker function"""
    print 'Worker thread: %s\n' % arg
    time.sleep(5)
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker, args=str(i), name="threadingPrac")
    threads.append(t)
    t.start()

Upvotes: 1

Related Questions