Ads 468x60px

##EasyReadMore##

15 1月, 2014

[Python] Threading Sample - 執行緒範例

每一秒加一個Thread
每一個Thread Sleep 10 seconds
Result:
sleeping 10 sec from thread 0
sleeping 10 sec from thread 1
sleeping 10 sec from thread 2
sleeping 10 sec from thread 3
sleeping 10 sec from thread 4
sleeping 10 sec from thread 5
sleeping 10 sec from thread 6
sleeping 10 sec from thread 7
sleeping 10 sec from thread 8
sleeping 10 sec from thread 9
finished sleeping from thread 0
finished sleeping from thread 1
finished sleeping from thread 2
finished sleeping from thread 3
finished sleeping from thread 4
finished sleeping from thread 5
finished sleeping from thread 6
finished sleeping from thread 7
finished sleeping from thread 8
finished sleeping from thread 9

 

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

 

"""

Threading Example

 

@author: Hank Wang <drapho@gmail.com>

@version: 20101230

"""

import time

from threading import Thread

 

def myfunc(i):

print("sleeping 10 sec from thread %d" % i)

time.sleep(10)

print("finished sleeping from thread %d" % i)

 

def main():

for i in range(10):

t = Thread(target=myfunc, args=(i,))

time.sleep(1)

t.start()

 

if __name__ == '__main__':

main()




 



http://whhnote.blogspot.tw/2010/12/python-threading-sample.html

0 意見:

張貼留言

 
Blogger Templates