Python3學習(3)-高級篇


 

 


  • 文件讀寫
    • 源文件test.txt
    • line1
      line2
      line3
    • 讀取文件內容
    • 1 f = open('./test.txt','r')#只讀模式打開test.txt文件
      2 print(f.read())#讀取文件的所有內容
      3 f.close()#關閉文件
    • 讀取文件內容(安全讀取try--finally)
    • 1 try:#添加try異常捕獲
      2     f = open('./test.txt','r')#只讀模式打開test.txt文件
      3     print(f.read())#讀取文件的所有內容
      4 finally:
      5     if f:    
      6         f.close()#關閉文件
    • 使用with簡化讀取代碼
    • 1 with open('./test.txt','r') as f:
      2     print(f.read())
      3     
    • 以上所有的執行結果均為
    • ➜  Python  python3 readTxt.py
      line1
      line2
      line3
    • 讀取方法詳解
      • read():讀取文件的所有內容。針對小文件
      • read(size):按指定大小來讀取文件的內容。size字節大小。針對大文件
      • readlines():按行來讀取文件的所有內容,返回為list格式。針對配制文件
    • 讀取模式
      • 'r':讀文件
      • 'rb':二進制讀文件
      • 'w':寫文件
      • 'wb':二進制寫文件
  • StringIO、BytesIO
    • StringIO:字符串IO
      1.  先從io中引入StringIO
      2. 創建一個StringIO對象
      3. 寫字符串到StringIO對象f中
      4. 獲取字符串內容f.getvalue()
      • >>> from io import StringIO
        >>> f  = StringIO()
        >>> f.write('hello')
        5
        >>> f.write(' ')
        1
        >>> f.write('world!')
        6
        >>> print(f.getvalue())
        hello world!
    • BytesIO
      1.  從io中引入BytesIO
      2. 創建一個BytesIO對象
      3. 寫字節對象
      4. 獲取寫入的字節內容內容
      • >>> from io import BytesIO
        >>> f = BytesIO()
        >>> f.write('我是中文'.encode('utf-8'))
        12
        >>> print(f.getvalue())
        b'\xe6\x88\x91\xe6\x98\xaf\xe4\xb8\xad\xe6\x96\x87'
  • 多進程
    • fork()
      • fork()調用一次,返回兩次,因為操作系統自動把當前進程(稱為父進程)復制了一份(稱為子進程),然后,分別在父進程和子進程內返回。
      • 子進程永遠返回0,而父進程返回子進程的ID。這樣做的理由是,一個父進程可以fork出很多子進程,所以,父進程要記下每個子進程的ID,而子進程只需要調用getppid()就可以拿到父進程的ID。
      •  1 import os
         2 
         3 print('Process (%s) starting...' % os.getpid())
         4 
         5 pid = os.fork()
         6 
         7 if pid == 0 :
         8     print('I am child process (%s) and my parent is %s' %(os.getpid(),os.getppid()))
         9 else:
        10     print('I (%s) just created a child process (%s)' %(os.getpid(),pid))
      • ➜  Python  python3 ThreadDemo.py
        Process (711) starting...
        I (711) just created a child process (712)
        I am child process (712) and my parent is 711
    • multiprocessing-Process
      •  1 from multiprocessing import Process
         2 import os
         3 
         4 #子進程代碼
         5 def run_proc(name):
         6     print('Run child process %s (%s).'%(name,os.getpid()))
         7 
         8 
         9 if __name__ == '__main__':
        10     print('Parent process %s.' % os.getpid())
        11     p = Process(target=run_proc,args = ('test',))
        12     print('Child process will start..')
        13     #啟動子進程
        14     p.start()
        15     #等待子進程結束后再繼續往下運行
        16     p.join()
        17     print('Child process end.')
      • ➜  Python  python3 MultiProcessins.py
        Parent process 718.
        Child process will start..
        Run child process test (719).
        Child process end.
    • Pool線程池
      •  1 from multiprocessing import Pool
         2 import os,time,random
         3 
         4 #子進程代碼
         5 def long_time_task(name):
         6     print('Run task %s (%s).' %(name,os.getpid()))
         7     start = time.time()
         8     time.sleep(random.random()*3)
         9     end = time.time()
        10     print('Task %s runs %.2f seconds.' %(name,(end-start)))
        11 
        12 
        13 if __name__ == '__main__':
        14     print('Parent process %s.'%os.getpid())
        15     #創建線程池
        16     p = Pool(4)
        17     for i in range(5):
        18         p.apply_async(long_time_task,args=(i,))
        19     print('Waiting for all subprocesses done..')
        20     p.close()
        21     p.join()
        22     print('All subprocesses done.')
      • ➜  Python  python3 Pool.py
        Parent process 730.
        Waiting for all subprocesses done..
        Run task 0 (731).
        Run task 1 (732).
        Run task 2 (733).
        Run task 3 (734).
        Task 2 runs 0.18 seconds.
        Run task 4 (733).
        Task 3 runs 0.83 seconds.
        Task 0 runs 1.18 seconds.
        Task 4 runs 2.46 seconds.
        Task 1 runs 2.66 seconds.
        All subprocesses done.
    • 子進程
      • 調用外部進程(系統進程):nslookup
      • 使用子進程
      • 1 import subprocess
        2 
        3 print('$ nslookup www.cnblogs.com')
        4 #調用外部
        5 r = subprocess.call(['nslookup','www.cnblogs.com'])
        6 print('Exit code:',r)
        ➜  Python  python3 SubProcess.py
        $ nslookup www.cnblogs.com
        Server:		10.1.1.5
        Address:	10.1.1.5#53
        
        Non-authoritative answer:
        Name:	www.cnblogs.com
        Address: 42.121.252.58
        
        Exit code: 0
      • 直接使用nslookup查看結果
      • ➜  ~  nslookup www.cnblogs.com
        Server:		10.1.1.5
        Address:	10.1.1.5#53
        
        Non-authoritative answer:
        Name:	www.cnblogs.com
        Address: 42.121.252.58
    • 多進程數據通信
      • 一個向Queue中寫數據,另一外讀數據
      •  1 from multiprocessing import Process,Queue
         2 import os,time,random
         3 
         4 #寫數據
         5 def write(q):
         6     print('Process to write:%s'%os.getpid())
         7     for value in ['A','B','C']:
         8         print('Put %s to queue.'%value)
         9         q.put(value)
        10         time.sleep(random.random())
        11 
        12 #讀數據
        13 def read(q):
        14     print('Process to read:%s'%os.getpid())
        15     while True:
        16         value = q.get(True)
        17         print('Get %s from queue.'%value)
        18 
        19 
        20 
        21 if __name__ == '__main__':
        22     q = Queue()
        23     pw = Process(target = write,args=(q,))
        24     pr = Process(target = read,args=(q,))
        25     pw.start()
        26     pr.start()
        27     pw.join()
        28     pr.terminate()
      • ➜  Python  python3 ProcessConn.py
        Process to write:803
        Put A to queue.
        Process to read:804
        Get A from queue.
        Put B to queue.
        Get B from queue.
        Put C to queue.
        Get C from queue.
    • 選擇
      • Unix/Linux下可使用fork()
      • 跨平台使用multiprocessing
      • 多進程數據通信Queue、Pipes
  • 多線程
    • 進程是由若干線程組成的,一個進程至少有一個線程。
      •  1 import time,threading
         2 
         3 #線程代碼
         4 def loop():
         5     print('thread %s is running..'%threading.current_thread().name)
         6     n = 0
         7     while n < 5:
         8         n = n + 1
         9         print('thread %s >>> %s' %(threading.current_thread().name,n))
        10         time.sleep(1)
        11     print('thread %s ended.'%threading.current_thread().name)
        12 
        13 print('thread %s is running.'%threading.current_thread().name)
        14 t = threading.Thread(target = loop,name = 'LoopThread')
        15 t.start()
        16 t.join()
        17 print('thread %s ended.'%threading.current_thread().name)
      • ➜  Python  python3 Thread.py
        thread MainThread is running.
        thread LoopThread is running..
        thread LoopThread >>> 1
        thread LoopThread >>> 2
        thread LoopThread >>> 3
        thread LoopThread >>> 4
        thread LoopThread >>> 5
        thread LoopThread ended.
        thread MainThread ended.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM