首页 > 代码库 > 使用 Python 进行线程编程

使用 Python 进行线程编程

w

 

 

使用 Python 进行线程编程
https://www.ibm.com/developerworks/cn/aix/library/au-threadingpython/index.html

import threading
import datetime
class ThreadClass(threading.Thread):
    def run(self):
        now = datetime.datetime.now()
        print "%s says Hello World at time: %s" % (self.getName(),now)
for i in range(2):
    t = ThreadClass()
    t.start()

 

 

C:\>python E:\MATHpyCLI\au-threadingpython.py
Thread-1 says Hello World at time: 2017-04-29 15:07:00.141000Thread-2 says Hello World at time: 2017-04-29 15:07:00.143000



C:\>python E:\MATHpyCLI\au-threadingpython.py
Thread-1 says Hello World at time: 2017-04-29 15:07:02.312000Thread-2 says Hello World at time: 2017-04-29 15:07:02.313000



C:\>python E:\MATHpyCLI\au-threadingpython.py
Thread-1 says Hello World at time: 2017-04-29 15:07:03.116000
 Thread-2 says Hello World at time: 2017-04-29 15:07:03.118000

 

使用 Python 进行线程编程