首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python的线程控制类

Python的线程控制类

原创
作者头像
用户7365393
修改2021-11-01 14:20:20
2870
修改2021-11-01 14:20:20
举报

下面是一个基础的python线程控制类

#!/usr/bin/env python
"""
testthread.py
An example of an idiom for controling threads
Doug Fort
http://www.dougfort.net
"""
import threading
class TestThread(threading.Thread):
    """
    A sample thread class
    """

def __init__(self):
    """
    Constructor, setting initial variables
    """
    self._stopevent = threading.Event()
    self._sleepperiod = 1.0

    threading.Thread.__init__(self, name="TestThread")

def run(self):
    """
    overload of threading.thread.run()
    main control loop
    """
    print "%s starts" % (self.getName(),)

    count = 0
    while not self._stopevent.isSet():
        count += 1
        print "loop %d" % (count,)
        self._stopevent.wait(self._sleepperiod)

    print "%s ends" % (self.getName(),)

def join(self,timeout=None):
    """
    Stop the thread
    """
    self._stopevent.set()
    threading.Thread.join(self, timeout)


if name == "main":
    testthread = TestThread()
    testthread.start()

import time
time.sleep(10.0)

testthread.join()

</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档