0

Following is the code in the Flask POST api endpoint.

df1 = pd.read_csv(r'postapi.csv')
df2 = {'user': user, 'target': target}
df1=df1.append(df2, ignore_index=True)
df1.to_csv(r'postapi.csv',index=False)

I want to store each endpoint request into a csv file.

The problem is that - when multiple requests are made, then writes are missed out. For example when 2 requests are fired, both will read 1 version of the file and end up appending their own record and saving the file. Thus one of the append will be missed in the final file.

Is there any way to do locking for read, append, write - so that when one endpoint request has read the csv then the other requests cannot read until write completes?

variable
  • 6,123
  • 5
  • 52
  • 127

0 Answers0