python 多线程编程_Python threading多线程编程实例

这篇文章主要介绍了Python

threading多线程编程实例,本文讲解了使用函数和线程类实现多线程编程的例子,需要的朋友可以参考下

Python 的多线程有两种实现方法:

函数,线程类

1.函数

调用 thread 模块中的 start_new_thread()

函数来创建线程,以线程函数的形式告诉线程该做什么

代码如下:

# -*- coding: utf-8 -*-

import thread

def f(name):

#定义线程函数

print "this is " name

if __name__ == '__main__':

thread.start_new_thread(f, ("thread1",))

#用start_new_thread()调用线程函数和其他参数

while 1:

pass

不过这种方法暂时没能找到其他辅助方法,连主线程等待都要用 while 1

这种方法解决。

2.线程类

调用 threading 模块,创建 threading.Thread

的子类来得到自定义线程类。

代码如下:

# -*- coding: utf-8 -*-

import threading

class Th(threading.Thread):

def __init__(self, name):

threading.Thread.__init__(self)

self.t_name

= name

#调

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值