成都创新互联网站制作重庆分公司

pythonthreading超线程使用简单范例的代码

在工作过程中中,将内容过程中经常用的内容片段珍藏起来,下面内容段是关于python threading超线程使用简单范例的内容,希望能对小伙伴们有较大帮助。

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都成都卫生间隔断小微创业公司专业提供成都企业网站定制营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。

# encoding: UTF-8
import threading

# 方法1:将要执行的方法作为参数传给Thread的构造方法
def func():
    print 'func() passed to Thread'

t = threading.Thread(target=func)
t.start()

# 方法2:从Thread继承,并重写run()
class MyThread(threading.Thread):
    def run(self):
        print 'MyThread extended from Thread'

t = MyThread()
t.start()

构造方法:Thread(group=None,target=None,name=None,args=(),kwargs={})group:线程组,目前还没有实现,库引用中提示必须是None;target:要执行的方法;name:线程名;args/kwargs:要传入方法的参数。实例方法:isAlive():返回线程是否在运行。正在运行指启动后、终止前。get/setName(name):获取/设置线程名。is/setDaemon(bool):获取/设置是否守护线程。初始值从创建该线程的线程继承。当没有非守护线程仍在运行时,程序将终止。start():启动线程。join([timeout]):阻塞当前上下文环境的线程,直到调用此方法的线程终止或到达指定的timeout(可选参数)。一个使用join()的例子:

# encoding: UTF-8
import threading
import time

def context(tJoin):
    print 'in threadContext.'
    tJoin.start()

    # 将阻塞tContext直到threadJoin终止。
    tJoin.join()

    # tJoin终止后继续执行。
    print 'out threadContext.'

def join():
    print 'in threadJoin.'
    time.sleep(1)
    print 'out threadJoin.'

tJoin = threading.Thread(target=join)
tContext = threading.Thread(target=context, args=(tJoin,))

tContext.start()

运行结果:

in threadContext. 
in threadJoin. 
out threadJoin. 
out threadContext.

本文题目:pythonthreading超线程使用简单范例的代码
网址分享:http://cxhlcq.com/article/jdcpjs.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部