tanggang1740 发表于 2017-4-30 10:08:41

python 多线程 和 多进程

  单线程例子:


#!/usr/bin/python
# -*- coding:UTF-8 -*-
# Name: danxiancheng.py
import time
import threading
def loop(num,sec):
print 'loop %s start: ....' % num, time.strftime('%Y-%M-%d %H:%m:%S')
time.sleep(sec)
print 'loop %s stop: .....' % num, time.strftime('%Y-%M-%d %H:%m:%S')

def main():
print '\nStarting: ........', time.strftime('%Y-%M-%d %H:%m:%S'), '\n'
mysec =
for i in range(len(mysec)):
loop(i,mysec)
print '\nAll DONE: ........', time.strftime('%Y-%M-%d %H:%m:%S'), '\n'
if __name__=='__main__':
main()
   打印结果:
  # python singlethread.py


Starting: ........ 2012-33-27 05:12:12


loop 0 start: .... 2012-33-27 05:12:12

loop 0 stop: ..... 2012-33-27 05:12:15

loop 1 start: .... 2012-33-27 05:12:15

loop 1 stop: ..... 2012-33-27 05:12:20

loop 2 start: .... 2012-33-27 05:12:20

loop 2 stop: ..... 2012-33-27 05:12:26

loop 3 start: .... 2012-33-27 05:12:26

loop 3 stop: ..... 2012-33-27 05:12:34


All DONE: ........ 2012-33-27 05:12:34


#
  多线程的例子:


#!/usr/bin/python
# -*- coding:UTF-8 -*-
# Name: duoxiancheng.py
import time
import threading
def loop(num,sec):
print 'loop %s start: ....' % num, time.strftime('%Y-%M-%d %H:%m:%S')
time.sleep(sec)
print 'loop %s stop: .....' % num, time.strftime('%Y-%M-%d %H:%m:%S')

def main():
print '\nStarting: ........', time.strftime('%Y-%M-%d %H:%m:%S'), '\n'
mysec =
mythreads = )) for i in range(len(mysec))]
for i in mythreads:
i.start()
for i in mythreads:
i.join()
print '\nAll DONE: ........', time.strftime('%Y-%M-%d %H:%m:%S'), '\n'
if __name__=='__main__':
main()
   执行结果:
  # python mutithread.py


Starting: ........ 2012-36-27 05:12:19


loop 0 start: .... 2012-36-27 05:12:19

loop 1 start: .... 2012-36-27 05:12:19

loop 2 start: .... 2012-36-27 05:12:19

loop 3 start: .... 2012-36-27 05:12:19

loop 0 stop: ..... 2012-36-27 05:12:22

loop 1 stop: ..... 2012-36-27 05:12:24

loop 2 stop: ..... 2012-36-27 05:12:25

loop 3 stop: ..... 2012-36-27 05:12:27


All DONE: ........ 2012-36-27 05:12:27
  结论:

  可以看到单线程中的两个循环, 只有一个循环结束后另一个才开始。而多线程则是进程内所有线程一起执行,同步任务。

  首先创建了n个线程,并将其存放到mythreads列表中,这里用到的是给Thread类传递了函数,第一个for循环是让两个线程开始执行。第二个for循环再让每个线程分别调用join函数,使程序挂起,直至相应线程都执行结束。
  多进程例子:
  test.py

#!/usr/bin/python
import time
import os
from os import environ

class MyClass:
def __init__(self):
self.aa = 1
def getA(self):
return environ['FOO']
def subA(self):
self.aa += 1

if __name__ == '__main__':
pass
   pro.py

#!/usr/bin/python
import os
import sys
import time, datetime, traceback
from os import environ
from multiprocessing import Process
from test import MyClass
def create_compute(hostname):
try:
environ['FOO'] = hostname
a = MyClass()
print a.getA()
except Exception as e:
print e
traceback.print_exc()
if __name__ == '__main__':
plist = []
for i in 4,5:
p = Process(target = create_compute, args = ('node'+str(i),))
plist.append(p);
p.start()
for p in plist:
p.join()
   执行结果:
  # python pro.py

node4

node5

#
  结论:
  两个进程中设置的环境变量 互不影响

多进程:

http://blog.csdn.net/inte_sleeper/article/details/6741963





多线程:




http://blog.chinaunix.net/uid-16362696-id-2746820.html
页: [1]
查看完整版本: python 多线程 和 多进程