0

다음과 같은 psuedo python 덩어리가 의미가 있음을 검증 할 몇 가지 눈알을 찾습니다. 가능한 한 빨리 inproc 함수를 구현하기 위해 많은 스레드를 생성하려고합니다.파이썬 내에서 기본 대기열/스레드 프로세스를 구현

chunk of code 
-get the filenames from a dir 
-write each filename ot a queue 
-spawn a thread for each filename, where each thread 
    waits/reads value/data from the queue 
-the threadParse function then handles the actual processing 
    based on the file that's included via the "execfile" function... 


# System modules 
from Queue import Queue 
from threading import Thread 
import time 

# Local modules 
#import feedparser 

# Set up some global variables 
appqueue = Queue() 

# more than the app will need 
# this matches the number of files that will ever be in the 
# urldir 
# 
num_fetch_threads = 200 


def threadParse(q) 
    #decompose the packet to get the various elements 
    line = q.get() 
    college,level,packet=decompose (line) 

    #build name of included file 
    fname=college+"_"+level+"_Parse.py" 
    execfile(fname) 
    q.task_done() 


#setup the master loop 
while True 
    time.sleep(2) 
    # get the files from the dir 
    # setup threads 
    filelist="ls /urldir" 
    if filelist 
    foreach file_ in filelist: 
     worker = Thread(target=threadParse, args=(appqueue,)) 
     worker.start() 

    # again, get the files from the dir 
    #setup the queue 
    filelist="ls /urldir" 
    foreach file_ in filelist: 
     #stuff the filename in the queue 
     appqueue.put(file_) 


    # Now wait for the queue to be empty, indicating that we have 
    # processed all of the downloads. 

    #don't care about this part 

    #print '*** Main thread waiting' 
    #appqueue.join() 
    #print '*** Done' 

감사합니다 생각/의견/포인터 ...

감사

아이디어는 마스터 루프에서 스레드를 생성하는 것입니다, 그래서 응용 프로그램은 병렬/동시 방식으로 동시에 스레드를 실행합니다

답변

0

이 권리를 이해하는 경우 : 일을 빨리 처리하기 위해 많은 스레드를 생성합니다.

각 스레드에서 수행 된 작업의 주요 부분이 GIL을 유지하지 않고 수행되는 경우에만 작동합니다. 따라서 네트워크, 디스크 또는 이와 유사한 데이터를 기다리는 데 많은 시간이 걸리는 경우 좋은 생각 일 수 있습니다. 각 작업이 많은 CPU를 사용하는 경우이 작업은 단일 코어 1-CPU 컴퓨터에서 실행되는 것과 거의 비슷하게 수행되며 순서대로 수행 할 수도 있습니다.

CPython에서는 필자가 쓴 내용이 사실이지만 Jython/IronPython에서는 필요하지 않다는 것을 덧붙여 야합니다. 또한 더 많은 CPU/코어를 사용해야하는 경우 도움이 될 multiprocessing 모듈이 있다고 덧붙여 야합니다.