perhaps you can use google spreadsheet's Protected Range feature as the lock. When person A wanna write data, he set the sheet as private, after that, set public. During person A's writing, if person B want to write also, he will meet exception, he can catch it, and wait a moment, later try to write into.
class ContextManagerUpdateSheet(object):
def __init__(self, spread_sheet_id, sheet_id):
self.spread_sheet_id = spread_sheet_id
self.sheet_id = sheet_id
# self.end_row_index = end_row_index
# self.end_column_index = ord(end_column_index) - 65
def __enter__(self):
logger.info("set spreadsheet sheet: {sheet_id} protected.")
self.protected_id = set_protected(self.spread_sheet_id, self.sheet_id)
def __exit__(self, *others):
logger.info("release protected spreadsheet sheet: {sheet_id}.")
delete_protected(self.spread_sheet_id, self.protected_id)
def runner():
with ContextManagerUpdateSheet("{google_spread_url}", 0):
from datetime import datetime
print datetime.now().strftime("%Y-%m-%d %H:%M:%s")
data = [["www", "3333"]]
apped_data("{google_spread_url}", 0, data)
---
@retry(googleapiclient.errors.HttpError, 5, 20, logger=logger)
def set_protected(spreadsheet_id, sheet_id):
logger.info("test")
service = get_service_handler()
requests_list = list()
requests_list.append(__protected_info(sheet_id))
body = {
"requests": requests_list
}
resp = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id,
body=body).execute()
return resp["replies"][0]["addProtectedRange"]["protectedRange"]["protectedRangeId"]