当前位置: 首页> 技术文章> python线程同步

python线程同步

Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。

threading 模块提供的其他方法:

  • threading.currentThread(): 返回当前的线程变量。

  • threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。

  • threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:

  • run(): 用以表示线程活动的方法。

  • start():启动线程活动。


  • join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。

  • isAlive(): 返回线程是否活动的。

  • getName(): 返回线程名。

  • setName(): 设置线程名。

#!/usr/bin/python# -*- coding: UTF-8 -*- import threadingimport time exitFlag = 0 class myThread (threading.Thread):   #继承父类threading.Thread    def __init__(self, threadID, name, counter):        threading.Thread.__init__(self)        self.threadID = threadID        self.name = name        self.counter = counter    def run(self):                   #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数        print "Starting " + self.name        print_time(self.name, self.counter, 5)        print "Exiting " + self.name def print_time(threadName, delay, counter):    while counter:        if exitFlag:            (threading.Thread).exit()        time.sleep(delay)        print "%s: %s" % (threadName, time.ctime(time.time()))        counter -= 1 # 创建新线程thread1 = myThread(1, "Thread-1", 1)thread2 = myThread(2, "Thread-2", 2) # 开启线程thread1.start()thread2.start() print "Exiting Main Thread"

上一篇: 2021年最值得测试同学使用的四种编程语言

下一篇: 软件测试之手工测试人员如何转测试开发?

QQ技术交流群

多测师官方学习交流
556733550

加入群聊