>  기사  >  백엔드 개발  >  Python의 멀티스레딩에 대한 자세한 설명

Python의 멀티스레딩에 대한 자세한 설명

Y2J
Y2J원래의
2017-04-28 09:47:471593검색

이 글에서는 Python 멀티스레딩 예제 관련 정보를 중심으로 자세히 소개하고 있습니다. 필요한 친구들은

Python 멀티스레딩 자세히 예제

를 참고하세요. 멀티스레딩은 일반적으로 더 많은 시간이 소요되는 작업을 처리하기 위해 새로운 백그라운드 스레드를 여는 것입니다. Python에서 백그라운드 스레드 처리를 수행하는 것도 매우 간단합니다. 오늘 공식에서 데모를 찾았습니다.

문서:

import threading, zipfile 
class AsyncZip(threading.Thread): 
  def __init__(self, infile, outfile): 
    threading.Thread.__init__(self) 
    self.infile = infile 
    self.outfile = outfile 
  def run(self): 
    f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) 
    f.write(self.infile) 
    f.close() 
    print('Finished background zip of:', self.infile) 
 
background = AsyncZip('mydata.txt', 'myarchive.zip') 
background.start() 
print('The main program continues to run in foreground.') 
 
background.join()  # Wait for the background task to finish 
print('Main program waited until background was done.')

결과:

The main program continues to run in foreground. 
Finished background zip of: mydata.txt 
Main program waited until background was done. 
Press any key to continue . . .

위 내용은 Python의 멀티스레딩에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.