考虑到稳定性,公司一直使用的是rhel5自带的版本python2.4.3.不久前,rhel5也将它的自带版本升级为python2.6.4版本了。
python最大的缺点在于服务器上有2.4和2.6+版本区别有些常用的东西需要2.6支持而rh6以下的linux都是带2.4的导致某些处理shell跨机通用性比python好
于是我浏览了下2.6版本的手册中的“what’s new”这一章节,选取了一部分在2.4中没有见到的语法和模块,或者同事之前用到比较少的模块,到公司培训室给同事讲了一下。以下为我将讲解的东西整理成了一份文字版本的。
— 条件表达式 语法
— x = true_value if condition else false_value
— 注意:需要有else语句
— 例子
— x = 2 if 2 < 3 else 1
— 类似c和php中的三目运算符
字符串格式化
— 2.4中 “%s 500wan %s” % (222, 444)
— 2.6中新增
— “{id}:{name}” .format(id =‘es427’,name =’xxx’)
— 2.4中只能try…except 或 try…finally
— 2.6则支持 try…except…finally或者try…except…else…finally
With语句
— 它是某些try…finally的简便替代用法
— 例子
— import sys
— fp = open("ifelse.py")
— print 'fp is',fp
— with fp:
— print fp.read()
— print 'fp is',fp
如果使用try…finally
— #这里的with语句相当于
— fp2 = open("ifelse.py")
— try:
— #do sth
— fp2.read()
— finally:
— fp2.close()
— With后面跟的对象需要有__enter__方法和 __exit__方法
— 例如刚才的fp 文件对象就有 __enter__ 和 __exit__
— 还可以使用的有theading的Lock对象和一些数据库连接等等。
— 总之紧接在with后面的语句需要有__enter__ 方法 和 __exit__
— with这一行语句开始的时候会自动调用对象的__enter__方法
— 整个with块语句执行完毕后会自动调用__exit__语句
— 例如:
— lock_object = threading.Lock()
— with lock_object: #这里实际执行了lock_object.acquire()
— print ‘do something’
— #with语句结束,自动调用lock_object.release()
生成唯一值的模块uuid
— 它有uuid() uuid2() uuid3() uuid4()
— >>> print uuid.uuid4()
— 8d0ad84b-3535-4028-8eca-c7e86dc22316
一个高性能的存储容器collections
— 其中Queue内部存储数据采用的deque
— deque对象 类似list 但不提供slice切片操作
— namedtuple对象 类似 tuple
— Defaultdict对象 类似 dict
优先级队列 后进先出队列
import Queue
Fq = Queue.Queue() 跟2.4是一样的
Pq = Queue.PriorityQueue() 优先级队列
Lq = Queue.LifoQueue() 后进先出队列
优先级队列存数据
Pq.put(2) 先把2放进去
Pq.put(1) 然后把1放进去
Pq.get()
Pq.get() 这时候1会先出来 然后是2
Pq.put( ( 3, [a,b,c]) )
Pq.put( (1,[d,2,3.4,a]))
Pq.get() (1,[d,2,3.4,a]) 会先出来
multiprocessing 模块
— 更高级的多进程模块
— 仿threading模块
— 提供跟threading类似的操作
— 更便利的进程间通信接口
— 它是非常庞大,非常有用的一个模块
子进程的新建和开启
- #coding=gbk
- '''''
- 使用Process创建和开启子进程
- '''
- from multiprocessing import Process
- import os
- from time import sleep
- def f(name):
- print 'hello mypid is ',os.getpid()
- print 'my parent pid is',os.getppid()
- sleep(30)
- print 'done' ,os.getpid()
- if __name__ == '__main__':
- print 'i am parent pid is',os.getpid()
- p = Process(target = f, args=('guozw',) )
- p.start()#帮你隐藏了底层的fork调用细节
- p.join() #等待子进程结束,隐藏系统调用wait或waitpid的细节
Multiprocessing提供的进程间通信方法
进程间通信之Queue
- #coding=gbk
- '''''
- 进程间通信之Queue
- '''
- #coding=gbk
- from multiprocessing import Process, Queue
- import os
- import time
- from time import sleep
- import random
- def a():
- '''''该函数会单独在一个进程中执行'''
- print 'my pid is',os.getpid()
- print 'my parent pid is',os.getppid()
- sleep(5)
- print theQueue.get(0)
- print theQueue.get(0)
- print theQueue.get(0)
- print 'pid %s done'%(os.getpid())
- def b():
- '''''该函数会单独在一个进程中执行'''
- print 'my pid is',os.getpid()
- print 'my parent pid is',os.getppid()
- sleep(3)
- print theQueue.get(0)
- print theQueue.get(0)
- print theQueue.get(0)
- print 'pid %s done'%(os.getpid())
- theQueue = Queue() #创建进程间通信的队列
- for i in range(1,6):
- theQueue.put(random.randint(1,30000000000)) #主进程向队列中写入
- print theQueue.qsize()
- ppa = Process( target = a ) #创建a进程
- ppb = Process( target = b) #创建b进程
- ppa.start() #创建a进程
- ppb.start() #创建b进程
- ppa.join() #等待a进程结束
- ppb.join() #等待b进程结束
进程间通信之Pipe
- #coding=gbk
- from multiprocessing import Process, Pipe
- import os
- def f(conn):
- '''''会单独在一个进程中执行的一个函数'''
- print 'my pid is',os.getpid()
- conn.send( ['the data of sending']) #在向管道中发送数据
- conn.close() #关闭管道的一端
- if __name__ == '__main__':
- parent_conn, child_conn = Pipe()
- p = Process(target = f, args=(child_conn,))
- p.start()
- print 'hereis parent',parent_conn.recv()
- p.join()
进程间通信之共享内存
- #coding=gbk
- '''''
- 进程间通信之共享内存
- '''
- from multiprocessing import Process, Value, Array
- from time import sleep
- def f(n,a):
- #设置修改了主进程的变量 n 和 a
- n.value = 333
- for i in range(len(a)):
- a[i] = -a[i]
- sleep(20)
- num = Value('d',0.0) #初始化共享变量num=0.0
- arr = Array('i',range(5)) #初始化共享变量arr=[1,2,3,4]
- p = Process( target = f, args = ( num, arr)) #创建子进程
- p.start() #运行子进程
- p.join() #等待子进程结束
- #子进程修改后num.value = 333, arr = [-1, -2,-3,-4]
- print num.value
- print arr[:]
multiprocessing还有很多非常有用的模块。具体参考手册。