Python学习笔记--多进程和多线程
"""多进程和多线程""" # 顺序执行 # from random import randint # from time import time, sleep # # # def download_task(filename): # print('开始下载%s...' % filename) # time_to_download = randint(5, 10) # sleep(time_to_download) # print('%s下载完成! 耗费了%d秒' % (filename, time_to_download)) # # # def main(): # start = time() # download_task('Python从入门到住院.pdf') # download_task('Peking Hot.avi') # end = time() # print('总共耗费了%.2f秒.' % (end - start)) # # # 开始下载Python从入门到住院.pdf... # # Python从入门到住院.pdf下载完成! 耗费了10秒 # # 开始下载Peking Hot.avi... # # Peking Hot.avi下载完成! 耗费了9秒 # # 总共耗费了19.02秒. # if __name__ == '__main__': # main() # 并行执行 # from multiprocessing import Process # from os import getpid # from random import randint # from time import time, sleep # # # def download_task(filename): # print('启动下载进程,进程号[%d].' % getpid()) # print('开始下载%s...' % filename) # time_to_download = randint(5, 10) # sleep(time_to_download) # print('%s下载完成! 耗费了%d秒' % (filename, time_to_download)) # # # #启动下载进程,进程号[9080]. # # 开始下载Peking Hot.avi... # # 启动下载进程,进程号[12668]. # # 开始下载Python从入门到住院.pdf... # # Python从入门到住院.pdf下载完成! 耗费了6秒 # # Peking Hot.avi下载完成! 耗费了8秒 # # 总共耗费了8.40秒. # def main(): # start = time() # p1 = Process(target=download_task, args=('Python从入门到住院.pdf', )) # p1.start() # p2 = Process(target=download_task, args=('Peking Hot.avi', )) # p2.start() # p1.join() # p2.join() # end = time() # print('总共耗费了%.2f秒.' % (end - start)) # # # if __name__ == '__main__': # main() # 用多线程 # from random import randint # from threading import Thread # from time import time, sleep # # # def download(filename): # print('开始下载%s...' % filename) # time_to_download = randint(5, 10) # sleep(time_to_download) # print('%s下载完成! 耗费了%d秒' % (filename, time_to_download)) # # # def main(): # start = time() # t1 = Thread(target=download, args=('Python从入门到住院.pdf',)) # t1.start() # t2 = Thread(target=download, args=('Peking Hot.avi',)) # t2.start() # t1.join() # t2.join() # end = time() # print('总共耗费了%.3f秒' % (end - start)) # # 开始下载Python从入门到住院.pdf... # # 开始下载Peking Hot.avi... # # Peking Hot.avi下载完成! 耗费了5秒 # # Python从入门到住院.pdf下载完成! 耗费了9秒 # # 总共耗费了9.008秒 # # if __name__ == '__main__': # main() # 自定义多线程 from random import randint from threading import Thread from time import time, sleep # class DownloadTask(Thread): # # def __init__(self, filename): # super().__init__() # self._filename = filename # # def run(self): # print('开始下载%s...' % self._filename) # time_to_download = randint(5, 10) # sleep(time_to_download) # print('%s下载完成! 耗费了%d秒' % (self._filename, time_to_download)) # # # def main(): # start = time() # t1 = DownloadTask('Python从入门到住院.pdf') # t1.start() # t2 = DownloadTask('Peking Hot.avi') # t2.start() # t1.join() # t2.join() # end = time() # print('总共耗费了%.2f秒.' % (end - start)) # # # 开始下载Python从入门到住院.pdf... # # 开始下载Peking Hot.avi... # # Peking Hot.avi下载完成! 耗费了5秒 # # Python从入门到住院.pdf下载完成! 耗费了10秒 # # 总共耗费了10.01秒. # if __name__ == '__main__': # main() from time import sleep from threading import Thread,Lock class Account(object): def __init__(self): self.lock = Lock() self._balance = 0 def deposit(self, money): self.lock.acquire() try: # 计算存款后的余额 new_balance = self._balance + money # 模拟受理存款业务需要0.01秒的时间 sleep(0.01) # 修改账户余额 self._balance = new_balance # self.lock.release() finally: self.lock.release() @property def balance(self): return self._balance class AddMoneyThread(Thread): def __init__(self, account, money): super().__init__() self._account = account self._money = money def run(self): self._account.deposit(self._money) def main(): account = Account() threads = [] # 创建100个存款的线程向同一个账户中存钱 for _ in range(100): t = AddMoneyThread(account, 1) threads.append(t) t.start() # 等所有存款的线程都执行完毕 for t in threads: t.join() print('账户余额为: ¥%d元' % account.balance) if __name__ == '__main__': main()