python从入门到放弃

python基础(五)

2018-02-13  本文已影响17人  EvanForEver

文本文件(包括CSV和JSON)的读写

>>f = open('sample.txt', 'r') 
#  r = read, w = write, a = append, b = binary, +表示文件不存在就创建(覆盖重写)
>>texts = f.read()
>>print(texts)
>>f.close()
Line #1 hello world
Line #2 life is not easy

#按行处理文件,加快速度显示 readline和readlines
>>with open('sample.txt', 'r') as f:
>>    line = f.readline() #读取下一行
>>    while line: #判断是否到结尾
>>       print(line.strip())
>>        line = f.readline()
Line #1 hello world
Line #2 life is not easy

>>with open('sample.txt', 'r') as f:
>>    for line in f.readlines(): #一次性读取多行
>>        print(line.strip())
Line #1 hello world
Line #2 life is not easy

#文件的写入
>>texts = ['New line #1 hello world', 'Line #2 life is not easy']
>>with open('new_sample.txt', 'w+') as f: # 覆盖文件重新写入
>>    for text in texts:
>>        f.write(text + '\n') #记得加换行符
>>with open('new_sample.txt', 'a') as f: #在文件结尾写入内容
>>    f.write('Something new\n')

json与csv文件操作

# json
>>import json
>>config = {'ip': '192.168.1.1', 'port': ['9100', '9101', '9102']}
>>with open('config.json', 'w+') as f:
>>    json.dump(config, f) # dump把字典写入json文件
>>with open('config.json', 'r') as f:
>>    new_config = json.load(f) 
>>print(type(new_config))
<class 'dict'>
>>print(new_config)
{'ip': '192.168.1.1', 'port': ['9100', '9101', '9102']}

#json文件相当于字典,但是里面字符串必须用双引号括起来
>>config_str = '{"ip": "192.168.1.1", "port": ["9100", "9101", "9102"]}'
>>config = json.loads(config_str)
>>print(config)
{'ip': '192.168.1.1', 'port': ['9100', '9101', '9102']}

>>new_config_str = json.dumps(config)
>>print(type(new_config_str))
<class 'str'>
>>print(new_config_str)
{"ip": "192.168.1.1", "port": ["9100", "9101", "9102"]}

序列化及应用

>>import pickle
>>class MyObject:
>>    def __init__(self, x, y):
>>        self.x = x
>>        self.y = y

>>obj = MyObject(100, 200)
>>s_obj = pickle.dumps(obj)
>>print(s_obj)
>>obj = pickle.loads(s_obj)
>>print(obj.x, obj.y)

多进程与多线程

# 多进程
from multiprocessing import Process
import os

# 子进程要执行的代码
def run_proc(name):
    print('Run child process %s (%s)...' % (name, os.getpid())) 
# 复制到文件然后在cmd窗口下执行,获取进程的id
# notebook只支持显示当前进程的

if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    p = Process(target=run_proc, args=('test',)) #target为进程的目标函数,args是参数
    #python中括号中不加逗号会以为是函数调用
    print('Child process will start.')
    p.start() # 开始进程
    p.join() # 等待进程执行结束
    print('Child process end.')

# 多线程
import time, threading
# python没有线程id但有线程名
# 新线程执行的代码:
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)

进程池与线程池

# 进程池
from multiprocessing import Pool
import os, time, random

def long_time_task(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 3)
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (name, (end - start)))

if __name__=='__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(4) #电脑有多少核可以设置相应两倍的进程数
    for i in range(5): #有5个任务,但具体怎么调度不用关系
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

# 线程池
import threadpool # 第三方库
import time

def long_op(x):
    print('%d\\n' % n)
    time.sleep(2)

pool = threadpool.ThreadPool(os.cpu_count()) # 决定
tasks = threadpool.makeRequests(long_op, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) #每个任务的参数
# 可以尝试函数使用多个参数,必须看源代码
print(len(tasks))
[pool.putRequest(task) for task in tasks]
pool.wait()

数据共享与锁

# 多进程变量共享
from multiprocessing import Process, Queue
import os, time, random

def write(q):
    print('Write: %s' % os.getpid())
    for value in ['AAA', 'BBB', 'Hello World']:
        print('Write %s' % value)
        q.put(value)
        time.sleep(random.random())
        
def read(q):
    print('Read: %s' % os.getpid())
    while True:
        value = q.get(True)
        print('Read %s' % value)
        
if __name__ == '__main__':
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    time.sleep(3)
    pr.terminate()
    print('Done')
# 锁
#避免多个线程同时进行导致数据丢失,使线程逐个进行
import threading

lock = threading.Lock()
balance = 0
def change_balance(n):
    global balance
    balance += n 
    # balance = 100,但是两个进程,1个加10,一个加20,同时操作,最后balance可能变成110,也可能变成120,
    # 但不是我们要的130。
    
def run_thread(n):
    lock.acquire() #执行这个进程时先锁上,避免其他线程打扰
    try:
        change_balance(n)
    except:
        pass
    finally:
        lock.release() # 一定要结合异常处理否则可能会导致锁无法释放
    
threads = []
for i in range(11):
    t = threading.Thread(target=run_thread, args=(i, ))
    threads.append(t)
for t in threads:
    t.start()
for t in threads:
    t.join()
print(balance)

系统库

上一篇 下一篇

猜你喜欢

热点阅读