여기 threading.Thread을 사용하는 방법의 예 : 여기
는 파이썬에서 스레딩에 대한 자습서입니다. 클래스 이름을 자신의 이름으로 바꾸고 실행 기능을 직접 바꿉니다. 스레딩은 자신의 것과 같은 IO 제한된 응용 프로그램에 적합하며 실제로 속도를 높일 수 있습니다. python 쓰레드를 엄격하게 표준 파이썬에서 계산하는 것은 도움이되지 않습니다. 왜냐하면 한 번에 하나의 쓰레드 만 계산할 수 있기 때문입니다.
import threading, time
class Ping(threading.Thread):
def __init__(self, multiple):
threading.Thread.__init__(self)
self.multiple = multiple
def run(self):
#sleeps 3 seconds then prints 'pong' x times
time.sleep(3)
printString = 'pong' * self.multiple
pingInstance = Ping(3)
pingInstance.start() #your run function will be called with the start function
print "pingInstance is alive? : %d" % pingInstance.isAlive() #will return True, or 1
print "Number of threads alive: %d" % threading.activeCount()
#main thread + class instance
time.sleep(3.5)
print "Number of threads alive: %d" % threading.activeCount()
print "pingInstance is alive?: %d" % pingInstance.isAlive()
#isAlive returns false when your thread reaches the end of it's run function.
#only main thread now
스레드 풀과 동일한 풀이 있습니까? 이것은 내가 필요로하는 것보다 더 무거운 별도의 프로세스를 실행하는 것처럼 보입니다. – Kiv
내장이없는 것 같지만, 이것을 발견했습니다 : http://www.chrisarndt.de/projects/threadpool/ 그리고 꽤 유사합니다. – drdaeman
고마워, threadpool 잘하고있다. – Kiv