multithreading in python

Dave Angel davea at davea.name
Tue Aug 13 07:22:56 EDT 2013


samaneh.yahyapour at gmail.com wrote:

> hi 
> my program work by 4 thread but when i use more thread it terminates
>
> 

I simplified your code so anybody could run it, and tested it inside
Komodo IDE, on Python 2.7

#!/usr/bin/env python


import sys
import os
import time
import threading

class MyClass():

    def __init__(self):
        i = 0
        while i<10:
            work = WorkClass(i)
            thread1 = threading.Thread(target=work.item_thread)
            thread1.start()
            i = i+1
            time.sleep(0.01)

class WorkClass():
    def __init__(self, parm):
        self.parm = str(parm)
    def item_thread(self):
        print "beginning thread", self.parm
        for filename in os.listdir("."):
            data =  "thread " +  self.parm + " filename " + filename + "\n"
            print data
            time.sleep(0.5)
        print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD " + self.parm

x = MyClass()         
print "Finishing main thread"

When the
-- 
Signature file not found




More information about the Python-list mailing list