2017-04-07 4 views
0

私はyelp fusion apiを使用してビジネスリスティングをプルしたい郵便番号のリストを持っています。各郵便番号は少なくとも1つのapi呼び出しを行う必要があります(しばしばはるかに多い)ので、毎日の制限が25000であるため、私のAPIの使用状況を把握できるようにしたいと思います。ユーザー定義のLocaleのインスタンスとして各郵便番号を定義しましたクラス。このロケールクラスには、クラス変数Locale.pullsがあります。これは、プル数のグローバルカウンタとして機能します。マルチスレッドのPythonスクレイピングにはロックが必要ですか?

マルチプロセッシングモジュールを使用してマルチスレッドを実行したいが、ロックを使用する必要があるかどうかわからない場合、どのようにすればよいだろうか?懸念事項は競合条件です。下の疑似コードで、各スレッドがZip.pullsクラス変数として定義されている現在のプル数を確認する必要があります。

import multiprocessing.dummy as mt 


class Locale(): 
    pulls = 0 
    MAX_PULLS = 20000 

    def __init__(self,x,y): 
     #initialize the instance with arguments needed to complete the API call 

    def pull(self): 
     if Locale.pulls > MAX_PULLS: 
      return none 
     else: 
      # make the request, store the returned data and increment the counter 
      self.data = self.call_yelp() 
      Locale.pulls += 1 


def main(): 
    #zipcodes below is a list of arguments needed to initialize each zipcode as a Locale class object 
    pool = mt.Pool(len(zipcodes)/100) # let each thread work on 100 zipcodes 
    data = pool.map(Locale, zipcodes) 

答えて

0

簡単な解決策はmap()を実行する前に、そのlen(zipcodes) < MAP_PULLSをチェックすることです。

関連する問題