E D R , A S I H C RSS

Python Multi Threading

Python 에서는 2가지 thread interface 를 제공한다. 하나는 C 스타일의 API 를, 하나는 Java 스타일의 Thread Object를.

사용하는 방법은 매우 간단. Thread class 를 상속받은뒤 Java 처럼 start 메소드를 호출해주면 run 메소드에 구현된 내용이 multithread 로 실행된다.

다음은 간단한 예.
~cpp 
import thread
import time

def runOne(args):
	i = 0
	while(1):
		i = i+1
		print "thread : ", i, args
		time.sleep(1)


if __name__=="__main__":
	thread.start_new_thread(runOne, ((1,)))

	for i in range(100000,0,-1):
		print "waiting: ", i
		time.sleep(1)

다른 차원의 기법으로는 Seminar:LightWeightThreads가 있다.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:08
Processing time 0.0089 sec