python 语法糖

       python通过apply使用元祖和列表调用函数

def my_fuc(a, b):
    print a, b

atuple=(30,10)
alist= ['Hello','World!']
apply(my_fuc,atuple)
apply(my_fuc,alist)
 

输出:

D:\py>python&nbspbuildin.py; 
30 10; 
Hello World;! 
用apply使用序列做参数来动态调用方法。

 

 

python通过线程实现定时器timer的代码,也可以改造定时任务(自调)

import threading
def sayhello():
        print "hello world"
        global t        #Notice: use global variable!
        t = threading.Timer(5.0, sayhello)
        t.start()

t = threading.Timer(5.0, sayhello)
t.start()


#该代码片段来自于: http://www.sharejs.com/codes/python/8815
1 2 3 4 5 6 7 8 9 10 11 12

运行结果如下

>python hello.py

hello world

hello world
 

python通过multiprocessing 实现带回调函数的异步调用

rom multiprocessing import Pool def f(x):return x*x if __name__ == '__main__': pool = Pool(processes=1) # Start a worker processes. result = pool.apply_async(f, [10], callback) # Evaluate "f(10)" asynchronously calling callback when finished.#该代码片段来自于: http://www.sharejs.com/codes/python/8362

猜你喜欢

转载自hugoren.iteye.com/blog/2322234