I/O 操作
input() ----3.x 版本取消了raw_input()---input函数只能返回字符串
open() ----函数返回一个打开的对象
read() ----方法读取打开的对象,但是读取一次后就不能再读取
write() ----给打开的对象写入内容
try:
except: ----如果try为真则不执行except 的值
try:
finally: ----如果try为真也要执行finally的值【读写文件的时候需要finally来释放内存close】
with open('a.txt',‘w’) as f:
f.write() ----##with 可以释放内存,也就是相当于多了一个f.close()
f.readlines()---##作为一个列表读取每一行
f.readline()---##只读取一行
操作文件和目录
os.getcwd() --##获得当前目录
os.remove()--##删除文件
os.chdir() ---##更改目录
序列化
存储变量一种是通过文件读写的方式,这种方式得控制格式。pickle模块可以直接把对象通过序列化存储下来,下次程序运行时再拿出来。
d=dict(url='index.html',title='shouye',content='pcikle')
f=open(r'c:\users\chenwen1\45.txt','wb')
pickle.dump(d,f)
f.close()
反序列化通过pickle.load()
多进程
multiprocessing
Process
多进程不能在python IDLE中运行
import os
import time
from multiprocessing import Process
def run_proc(name):
print('Child process %s(%s) Running...'%(name,os.getpid())) ##这种输出方式
if __name__=='__main__':
print('Parent process %s.'%os.getpid()) ##os.getpid()获得进程号
for i in range(5):
p=Process(target=run_proc,args=(str(i),)) ##生成一个新的进程【target是进程的事情,args是函数参数是元组】
print('Process will start')
p.start() ##开始进程
p.join() ##阻止主程序运行
print('Process end')
***如果是没有p.join(),则会主程序运行完成以后再运行进程***
Pool库(子进程资源库)
import os
import time
import random
from multiprocessing import Pool
def run_task(name):
print('Task %s(pid=%s) is running...'%(name,os.getpid()))
time.sleep(1)
print('Task %s end'%name)
if __name__=='__main__':
print('Current process %s'%os.getpid())
p=Pool(processes=3) #子进程最多运行3个
for i in range(5):
p.apply_async(run_task , args=(str(i),)) #开始子进程
print('waiting foa all subprocesses done...')
p.close() # 资源池不能再添加新的子进程【会把self.state的状态改为close,join函数运行的前提是状态不为run】
p.join() #主进程等待子进程
print('ALL subprocesses done')
进程间的通信
通过队列(Queue)
import os
import time
import random
from multiprocessing import Process , Queue
def proc_write(q,urls):
print('Process(%s) is Running..'%os.getpid())
for url in urls:
q.put(url) #往队列中加入对象
print('Put %s to queue...'%url)
time.sleep(random.random())
def proc_read(q):
print('Process(%s) is reading...'%os.getpid())
while True:
url=q.get(True) #从队列中读取对象并删除
print('Get %s from queue.'%url)
if __name__=='__main__':
q=Queue() #创建队列再进程中
proc_write1 = Process(target = proc_write,args=(q,['url_1','url_2','url_3'])) #队列对象作为参数传给子进程
proc_write2 = Process(target = proc_write,args=(q,['url_4','url_5','url_6']))
proc_reader = Process(target = proc_read,args=(q,))
proc_write1.start()
proc_write2.start()
proc_reader.start()
proc_write1.join()
proc_write2.join()
proc_reader.terminate()
通过管道(Pipe)
import multiprocessing
import random
import time,os
def proc_send(pipe,urls):
for url in urls:
print('Procesing(%s)send:%s'%(os.getpid(),url))
pipe.send(url)##向管道中送入数据
time.sleep(random.random())
def proc_recv(pipe):
while True:
print('Process(%s) rev.%s'%(os.getpid(),pipe.recv()))##从管道中接收数据
time.sleep(random.random())
if __name__ == '__main__':
pipe = multiprocessing.Pipe()##创建管道
p1=multiprocessing.Process(target = proc_send,args=(pipe[0],['url_'+str(i) for i in range(10)]))##使用第一个参数
p2=multiprocessing.Process(target=proc_recv,args=(pipe[1],))##使用第二个参数
p1.start()
p2.start()
p1.join()
p2.join()
多线程
第一种和多进程有点像
import random
import time
import threading
def thread_run(urls):
print('Current %s is running...'%threading.current_thread().name)
for url in urls:
print('%s ---->>>>%s'%(threading.current_thread().name,url))
time.sleep(random.random())
print('%s ended'%threading.current_thread().name)
print('%s is running...'%threading.current_thread().name)
t1=threading.Thread(target=thread_run,name='Thread_1',args=(['url_1','url_2','url_3'],))#创建
t2=threading.Thread(target=thread_run,name='Thread_2',args=(['url_4','url_5','url_6'],))#创建
t1.start()
t2.start()
t1.join()
t2.join()
第二种利用类的方法:
首先介绍类
类的继承(比如student类继承people类)
class Student(people):
def __init__(self,name,age,sex,salary):
people.__init__(self,name,age,sex)
self.salary=salary
import random
import threading
import time
class myThread(threading.Thread):
def __init__(self,name,urls):
threading.Thread.__init__(self,name=name) ##这里相当于把前面那几行用这一行代替
self.urls=urls
def run(self):
print('Current %s is running...'%threading.current_thread().name)
for url in self.urls: ##这里必须用self.urls
print('%s ---->>>>%s'%(threading.current_thread().name,url))
time.sleep(random.random())
print('%s ended'%threading.current_thread().name)
print('%s is runnin...'%threading.current_thread().name)
t1 = myThread(name='Thread_1', urls=['url_1', 'url_2', 'url_3'])
t2 = myThread(name='Thread_2', urls=['url_4', 'url_5', 'url_6'])
t1.start()
t2.start()
t1.join()
t2.join()
网友评论