2016-10-30 4 views
0

私はPython(Django)上でRESTサービスを作成していますが、このサービスはそのAPIによって別のRESTサービスに組み込む必要があります。Python並列APIリクエスト処理の方法は?

ここでラインのいくつかのコードと時間:

connection = statServer("myname", "mypassword") 

q1 = connection.getJSONdict("query1") # approximately 15 seconds 
q2 = connection.getJSONdict("query2") # approximately 20 seconds 
q3 = connection.getJSONdict("query3") # approximately 15 seconds 

# my processing approximately 0.01 of second 
# merge q1 + q2 + q3 

それはそれはしないように、各要求getJSONdict(「クエリ」)はは、実際に、離れてI/Oを待っているから何もしないことを私には明らかですプロセッサ時間を消費する。

リクエストは順番に実行されるため、リクエストを別々のスレッドで実行できます。私は、Pythonが実際のスレッドを提供していないと言われていますが、私の場合はI/Oを待っているので、スレッド化のようなことをすることができます。

私はこれが実際にはPythonのユーザケースであると思います。このタスクのようなものを扱っていれば、私の解決に役立ちます。


私はフォークについての考えを持っている/フレームワークまたはより良い入会私のRESTサービスにおけるすべての要求からの私の要求(とスレッドを再利用するために)を消費するThreadExecutorPullになります。


答えて

2

私は自分でそれを行うことができました。

from multiprocessing.pool import Pool, ThreadPool 
# ... others imports 

# You can dicede here to use processes or threads, 
# if you want threads change Pool() to ThreadPool() 
pool = Pool() 
connection = statServer("myname", "mypassword") 

res = pool.map(connection.getJSONdict, ["query1", "query2", "query3"]) 
print(res) 
関連する問題