python线程任务run

Python Thread.run()方法 (Python Thread.run() Method)

Thread.run() method is an inbuilt method of the Thread class of the threading module in Python. This method is used to represent a thread's activity. It calls the method expressed as the target argument in the Thread object along with the positional and keyword arguments taken from the args and kwargs arguments, respectively. This method can also be overridden in the subclass.

Thread.run()方法是Python中线程模块的Thread类的内置方法。 此方法用于表示线程的活动。 它调用在Thread对象中表示为目标参数的方法,以及分别从argskwargs参数获取的position和关键字参数。 也可以在子类中重写此方法。

Module:

模块:

    from threading import Thread

Syntax:

句法:

    run()

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is <class 'NoneType'>, it returns nothing.

此方法的返回类型为<class'NoneType'> ,它什么也不返回。

Example:

例:

# Python program to explain the
# use of run() method in Thread class
import threading
def thread_1(i):
print('Value by Thread 1:', i)
def thread_2(i):
print('Value by Thread 2:', i)
def thread_3(i):
print('Value by Thread 3:', i)
# Creating three sample threads
thread1 = threading.Thread(target=thread_1, args=(1,))
thread2 = threading.Thread(target=thread_2, args=(2,))
thread3 = threading.Thread(target=thread_3, args=(3,))
# Running three thread object
thread1.run()
thread2.run()
thread3.run()

Output

输出量

Value by Thread 1: 1
Value by Thread 2: 2
Value by Thread 3: 3

run() method can also be overridden in the subclass. Given below creates a subclass of the Thread class and overrides the run function.

run()方法也可以在子类中重写。 下面给出的创建Thread类的子类并覆盖run函数。

Example:

例:

# Python program to demonstrate
# the overriding of run() method
import threading
class mythread(threading.Thread):
def __init__(self, thread_name, thread_ID):
threading.Thread.__init__(self)
self.thread_name = thread_name
self.thread_ID = thread_ID
# Overrriding of run() method in the subclass
def run(self):
print("Thread name: "+str(self.thread_name) +"  "+ "Thread id: "+str(self.thread_ID));
thread1 = mythread("thread1", 1)
thread2 = mythread("thread2", 2);
thread1.start()
thread2.start()

Output

输出量

Thread name: thread1  Thread id: 1
Thread name: thread2  Thread id: 2

翻译自: https://www.includehelp.com/python/thread-run-method-with-example.aspx

python线程任务run

python线程任务run_Python线程类| 带有示例的run()方法相关推荐

  1. python wait之后怎么起起来_python wait方法_Python条件类| 带有示例的wait()方法

    python wait方法 Python Condition.wait()方法 (Python Condition.wait() Method) wait() is an inbuilt method ...

  2. python wait方法_Python条件类| 带有示例的wait()方法

    python wait方法 Python Condition.wait()方法 (Python Condition.wait() Method) wait() is an inbuilt method ...

  3. python线程任务run_python线程、进程知识梳理

    一.python线程 线程用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/ usr / bin / env python # - * - coding:utf-8 - * - impo ...

  4. Python 批量创建线程及threading.Thread类的常用函数及方法

    在<[Python]线程的创建.执行.互斥.同步.销毁>(点击打开链接)中介绍了Python中线程的使用,但是里面线程的创建,使用了很原始的方式,一行代码创建一条.其实,Python里是可 ...

  5. acquire方法_Python锁类| 带有示例的acquire()方法

    acquire方法 Python Lock.acquire()方法 (Python Lock.acquire() Method) acquire() is an inbuilt method of t ...

  6. python 类方法装饰器_python类装饰器即__call__方法

    上一篇中我对学习过程中的装饰器进行了总结和整理,这一节简单整理下类装饰器 1.类中的__call__方法: 我们在定义好一个类后,实例化出一个对象,如果对这个对象以直接在后边加括号的方式进行调用,程序 ...

  7. Java中Thread类的start()和run()方法

    1.两个方法的区别 start() : 它的作用是启动一个新线程,新线程会执行相应的run()方法.start()不能被重复调用. run()   : run()就和普通的成员方法一样,可以被重复调用 ...

  8. Python HTMLCalendar类| 带有示例的formatyearpage()方法

    Python HTMLCalendar.formatyearpage()方法 (Python HTMLCalendar.formatyearpage() Method) formatyearpage( ...

  9. python调用带参函数_Python | 带有示例的函数调用类型

    python调用带参函数 There are following types of function calls in python: python中有以下类型的函数调用: Call by value ...

最新文章

  1. DM9000 寄存器的定义
  2. 30-seconds-code——math
  3. 24有几种封装尺寸_Y6T16 光模块尺寸演进
  4. H - Maximal submatrix HDU - 6957
  5. 关于猿如何找对象,心里没点那啥数吗?
  6. 18 CO配置-控制-产品成本控制-产品成本计划编制-定义估价变式
  7. Vivado封装自定义IP
  8. ubuntu c/c++ 生成.so 并被python调用
  9. 苹果电脑怎么进入虚拟服务器设置,苹果Mac电脑 Vmware虚拟机共享文件夹设置教程...
  10. openrasp-iast 灰盒扫描工具
  11. 测度论与概率论笔记6:符号测度
  12. revit2016对应lumion版本_revit如何导入lumion?revit文件转化成lumion文件有诀窍!
  13. π的值(已算到6086位)
  14. linux管理账户是,Linux管理账户
  15. ROG 幻16无线网卡驱动无法更新
  16. 俞敏洪一分钟励志演讲:
  17. 【线性代数】机器学习·算法必备 线性代数总结
  18. 《HCNA网络技术学习指南》-命令版
  19. @loj - 2289@「THUWC 2017」在美妙的数学王国中畅游
  20. Pair 智能标注神器,医生的好帮手

热门文章

  1. java中对象多态时成员变量,普通成员函数及静态成员函数的调用情况
  2. mount --bind的用处
  3. 计算机-p命令,OD(电脑命令)_百度百科
  4. python编程制作接金币游戏_一个简单的pygame接金币游戏
  5. openssl 生成证书_CentOS7 httpd(Apache)SSL 证书部署
  6. python3.6安装ipython_centos6.5下安装python3.6、pip、ipython
  7. java获取界面输入数字_通过JAVA设计 GUI 界面的计算器程序,用户可以通过鼠标依次输入参加计算的数值,进行加、减、乘、...
  8. Docker挂了,数据如何找回
  9. MySQL:基本命令
  10. JDK源码解析之 Java.lang.Compiler