0

次の疑似 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'

考え/コメント/ポインタは大歓迎です...

ありがとう

4

1 に答える 1

0

私がこれを正しく理解していれば、多くのスレッドを生成して、物事をより速く完了できます。

これは、各スレッドで実行されるジョブの主要部分が GIL を保持せずに実行される場合にのみ機能します。そのため、ネットワークやディスクなどからのデータを大量に待機している場合は、良い考えかもしれません。各タスクが大量の CPU を使用している場合、これはシングル コア 1 CPU マシンの場合とほとんど同じように実行され、順番に実行することもできます。

私が書いたことは CPython には当てはまりますが、Jython/IronPython には必ずしも当てはまりません。また、より多くの CPU/コアを利用する必要がある場合は、マルチプロセッシングモジュールが役立つ可能性があることを付け加えておきます。

于 2010-07-28T20:21:57.930 に答える