多线程(2)

 1 # 线程共享变量
 2 # 多线程和多进程不同之处在于多线程本身就是可以和父进程共享内存的,
 3 # 这也是为什么其中一个线程挂掉以后,为什么其他线程也会死掉的道理。
 4 
 5 import threading
 6 l = list()
 7 l += range(1, 10)
 8 def worker():
 9     l.append("ling")
10     l.append("shang")
11     l.append("hello")
12 if __name__ == "__main__":
13     t = threading.Thread(target=worker)
14     t.start()
15     print(l)
16 
17 
18 
19 # 线程池(了解即可)
20 # 通过传入一个参数组来实现多线程,并且它的多线程是有序的,顺序与参数组中的参数顺序保持一致。
21 # 安装包:
22 # pip install threadpool
23 import threadpool
24 def hello(m, n, o):
25     """"""
26     print "m = %s, n = %s, o = %s" % (m, n, o)
27 
28 if __name__ == '__main__':
29 
30 # 方法1
31     lst_vars_1 = ['1', '2', '3']
32     lst_vars_2 = ['4', '5', '6']
33     func_var = [(lst_vars_1, None), (lst_vars_2, None)]
34  # 方法2
35     dict_vars_1 = {'m': '1', 'n': '2', 'o': '3'}
36     dict_vars_2 = {'m': '4', 'n': '5', 'o': '6'}
37     func_var = [(None, dict_vars_1), (None, dict_vars_2)]
38     pool = threadpool.ThreadPool(2)
39 
40     requests = threadpool.makeRequests(hello, func_var)
41     [pool.putRequest(req) for req in requests]
42     pool.wait()

 

posted @ 2017-12-15 20:32  依哈  阅读(116)  评论(0)    收藏  举报