python 对类的成员函数开启线程的方法

yipeiwu_com6年前Python基础

如下所示:

# -*- coding: utf-8 -*-
import threading
import thread
import time


class Test(object):
  def __init__(self):
    # threading.Thread.__init__(self)
    self._sName = "machao"

  def process(self):
    #args是关键字参数,需要加上名字,写成args=(self,)
    th1 = threading.Thread(target=Test.buildList, args=(self,))
    th1.start()
    th1.join()

  def buildList(self):
    while True:
      print "start"
      time.sleep(3)


test = Test()
test.process()

以上这篇python 对类的成员函数开启线程的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python转换字符串为摩尔斯电码的方法

本文实例讲述了python转换字符串为摩尔斯电码的方法。分享给大家供大家参考。具体实现方法如下: chars = ",.0123456789?abcdefghijklmnop...

Python中的模块和包概念介绍

模块概述 如果说模块是按照逻辑来组织 Python 代码的方法, 那么文件便是物理层上组织模块的方法。 因此, **一个文件被看作是一个独立模块, 一个模块也可以被看作是一个文件。 模...

python打印异常信息的两种实现方式

1. 直接打印错误 try: # your code except KeyboardInterrupt: print("quit") except Excepti...

Python编码时应该注意的几个情况

在编程过程中,多了解语言周边的一些知识,以及一些技巧,可以让你加速成为一个优秀的程序员。对于Python程序员,你需要注意一下本文所提到的这些事情。你也可以看看Zen of Python...

pandas实现选取特定索引的行

如下所示: >>> import numpy as np >>> import pandas as pd >>> index=n...