标签:div 写入 counter 封装 循环 queue fifo key and
多进程:
fork()调用一次,返回两次,因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),
然后,分别在父进程和子进程内返回getppid()得到父进程的IDgetpid() 得到当前进程的ID
# multiprocessing.py
import os
print ‘Process (%s) start...‘ % os.getpid()
pid = os.fork()
if pid==0:
print ‘I am child process (%s) and my parent is %s.‘ % (os.getpid(), os.getppid())
else:
print ‘I (%s) just created a child process (%s).‘ % (os.getpid(), pid)
Process (876) start...
I (876) just created a child process (877).
I am child process (877) and my parent is 876.
进程之间的通信:
from multiprocessing import Process, Queue
import os, time, random
# 写数据进程执行的代码:
def write(q):
for value in [‘A‘, ‘B‘, ‘C‘]:
print ‘Put %s to queue...‘ % value
q.put(value)
time.sleep(random.random())
# 读数据进程执行的代码:
def read(q):
while True:
value = q.get(True)
print ‘Get %s from queue.‘ % value
if __name__==‘__main__‘:
# 父进程创建Queue,并传给各个子进程:
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
# 启动子进程pw,写入:
pw.start()
# 启动子进程pr,读取:
pr.start()
# 等待pw结束:
pw.join()
# pr进程里是死循环,无法等待其结束,只能强行终止:
pr.terminate()
多线程:
Python的标准库提供了两个模块:thread和threading,thread是低级模块,threading是高级模块,对thread进行了封装。
绝大多数情况下,我们只需要使用threading这个高级模块。
启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行:
#coding=utf-8
import time, threading
# 新线程执行的代码:
def loop():
print ‘thread %s is running...‘ % threading.current_thread().name
n = 0
while n < 5:
n = n + 1
print ‘thread %s >>> %s‘ % (threading.current_thread().name, n)
time.sleep(1)
print ‘thread %s ended.‘ % threading.current_thread().name
print ‘thread %s is running...‘ % threading.current_thread().name
t = threading.Thread(target=loop, name=‘LoopThread‘)
t.start()
t.join()
print ‘thread %s ended.‘ % threading.current_thread().name
。。。。。。。
collections模块提供了一些有用的集合类,可以根据需要选用。
使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict:
#coding=utf-8 from collections import defaultdict dd = defaultdict(lambda: ‘N/A‘) dd[‘a‘] = 123 print dd[‘a‘] print dd[‘b‘]
使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。
如果要保持Key的顺序,可以用OrderedDict:
OrderedDict可以实现一个FIFO(先进先出)的dict,当容量超出限制时,先删除最早添加的Key:
。。。。。。。。。。。。。。
base64
>>> base64.b64encode(‘i\xb7\x1d\xfb\xef\xff‘) ‘abcd++//‘ >>> base64.urlsafe_b64encode(‘i\xb7\x1d\xfb\xef\xff‘) ‘abcd--__‘ >>> base64.urlsafe_b64decode(‘abcd--__‘) ‘i\xb7\x1d\xfb\xef\xff‘
python计数器Count
# -*- coding:utf-8 -*-
"""
python计数器Counter
需导入模块collections
"""
import collections
# 统计各个字符出现的次数,以字典形式返回
obj = collections.Counter(‘adfsdfsdfswrwerwegfhgfhgh‘)
print obj
# elements => 原生的传入的值(‘adfsdfsdfswrwerwegfhgfhgh‘)
for v in obj.elements():
print v
# 按参数给定的个数返回
print obj.most_common(4)
# 执行结果显示 Counter({‘f‘: 5, ‘d‘: 3, ‘g‘: 3, ‘h‘: 3, ‘s‘: 3, ‘w‘: 3, ‘e‘: 2, ‘r‘: 2, ‘a‘: 1}) [(‘f‘, 5), (‘d‘, 3), (‘g‘, 3), (‘h‘, 3)]
请写一个能处理 去掉=的base64解码函数:
import base64
text = ‘YWJjZA‘
if not len(text)%4==0:
print base64.b64decode(text+"="*(len(text)%4))
struct的pack函数把任意数据类型变成字符串:
>>> import struct >>> struct.pack(‘>I‘, 10240099) ‘\x00\x9c@c‘
unpack把str变成相应的数据类型:
>>> struct.unpack(‘>IH‘, ‘\xf0\xf0\xf0\xf0\x80\x80‘) (4042322160, 32896)
标签:div 写入 counter 封装 循环 queue fifo key and
原文地址:http://www.cnblogs.com/Bgod/p/6985194.html