Python多线程

忍无可忍,今天把python3.1换到了2.7,不然每次发现写个代码编译不过我就得去查这是不是又是3.0的改变之处!我还是个初学者,没那么多时间在那上纠结!

 

 

Why multi-thread?

  • Multiple threads within a process share the same data space with the main thread and can therefore share information or communicate with each other more easily than if they were separate processes.

  • Threads sometimes called light-weight processes and they do not require much memory overhead; theycare cheaper than processes.

启动一个线程:

thread.start_new_thread ( function, args[, kwargs] )



#!/usr/bin/python

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print "Error: unable to start thread"

while 1:
pass


  • run(): The run() method is the entry point for a thread.

  • start(): The start() method starts a thread by calling the run method.

  • join([time]): The join() waits for threads to terminate.

  • isAlive(): The isAlive() method checks whether a thread is still executing.

  • getName(): The getName() method returns the name of a thread.

  • setName(): The setName() method sets the name of a thread.

未完待续。。。。。。。。。。。。。。。。。。。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值