我在我的app.yaml中使用Google App Engine 1.7.4上的Python 2.7運行時與threadsafe: true
。我是否需要在Google App Engine中使用線程同步工具?
我有以下代碼:
@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
可能被執行的另一條線的yield
操作呈現object not in shared_container
聲明可能無效的過程中被更新。然而,
if object not in shared_container:
shared_container.add(object)
yield any tasklet operation
將是絕對好的。
這個。他解釋說它很好 –