app.yamlでGoogleAppEngine1.7.4でPython2.7ランタイムを使用してthreadsafe: trueいます。
私は次のコードを持っています:
@ndb.tasklet
def fn_a(container):
''' access datastore and mutate container '''
@ndb.tasklet
def fn_b(container):
''' access datastore and mutate container '''
@ndb.toplevel
def parallel_fn():
shared_container = set()
yield fn_a(shared_container), fn_b(shared_container)
fn_a()アクセスとfn_b()変異の両方shared_containerがあり、で呼び出されparallel_fn()ます。shared_containerは標準ライブラリsetであるため、スレッドセーフではありません。
のミューテイタ/アクセサメソッドをshared_container適切なthreading標準ライブラリロックでラップする必要がありますか?
App Engineについて私が理解していることから、設定にもかかわらず、各インスタンスはシングルスレッドですthreadsafe: true。したがって、threadingロックオブジェクトの使用は必要ありませんか?
予備テストでは、ロックは不要であり、デッドロックだけでなくオーバーヘッドも追加されることが示されています。また、次のことはすべきではないようです
if object not in shared_container:
yield any tasklet operation
shared_container.add(object)
操作shared_container中に別の実行行によって更新される可能性があるため、ステートメントが無効になる可能性があります。でもyieldobject not in shared_container
if object not in shared_container:
shared_container.add(object)
yield any tasklet operation
絶対に大丈夫でしょう。