Python之多线程
添加时间:2013-5-28 点击量:
MyThread类是我本身实现的一个类,持续自threading模块中的Thread类,在子类中重写run办法,当过程调用start办法时辰,子类的run办被调用!工作须要,现学现卖,献丑了!
Created on May 28, 2013
@author: Berlin
import threading
class MyThread(threading.Thread):
def __init__(self, myId, count, mutex):
self.myId = myId
self.count = count
self.mutex = mutex
threading.Thread.__init__(self)
def run(self):
for i in range(self.count):
with self.mutex:
print([%s] => %s % (self.myId, i))
def Main():
stdoutmutex = threading.Lock()
threads = []
for i in range(10):
thread = MyThread(i, 100, stdoutmutex)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print(Main thread exiting.)
if __name__ == __main__:
Main()
感谢浏览!
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永
MyThread类是我本身实现的一个类,持续自threading模块中的Thread类,在子类中重写run办法,当过程调用start办法时辰,子类的run办被调用!工作须要,现学现卖,献丑了!
Created on May 28, 2013
@author: Berlin
import threading
class MyThread(threading.Thread):
def __init__(self, myId, count, mutex):
self.myId = myId
self.count = count
self.mutex = mutex
threading.Thread.__init__(self)
def run(self):
for i in range(self.count):
with self.mutex:
print([%s] => %s % (self.myId, i))
def Main():
stdoutmutex = threading.Lock()
threads = []
for i in range(10):
thread = MyThread(i, 100, stdoutmutex)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print(Main thread exiting.)
if __name__ == __main__:
Main()
感谢浏览!
容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永