Python模块threading

  • 介绍
threading提供了一个比thread模块更高层的API来提供线程的并发性。这些线程并发运行并共享内存。
  • 示例
延时执行函数 [codesyntax lang="python"]
!/usr/bin/python
# from http://surenpi.com

from threading import Timer
import time

def timer_fun():
        print 'timer function'

t = Timer(1, timer_fun)
t.start()

while True:
        time.sleep(0.1)
        print 'main running'
[/codesyntax] 多线程并发 [codesyntax lang="python"]
!/usr/bin/python
# from http://surenpi.com

import threading
import time

def worker():
        print 'worker'

for i in xrange(5):
        t = threading.Thread(target = worker)
        t.start()
[/codesyntax]

你可能感兴趣的:(多线程,并发,python,threading)