Skip to content

Instantly share code, notes, and snippets.

@ShahriyarR
Created April 23, 2024 19:57
Show Gist options
  • Save ShahriyarR/6c38d6d749d043181bfbf8b57b12fb1b to your computer and use it in GitHub Desktop.
Save ShahriyarR/6c38d6d749d043181bfbf8b57b12fb1b to your computer and use it in GitHub Desktop.
from flask import Flask, request
import threading
import time
app = Flask(__name__)
# Mock database storing user data
users = {
'1': {'email': 'user1@example.com'},
'2': {'email': 'user2@example.com'}
}
# Shared variable for email updates
temporary_email_storage = ""
@app.route('/change_email/<user_id>', methods=['POST'])
def change_email(user_id):
global temporary_email_storage
new_email = request.form['email']
print(f"Received request to change email for user {user_id} to {new_email}")
# Store the email in a shared variable
temporary_email_storage = new_email
# Simulate a delay to enhance the chance of race conditions
time.sleep(1)
# Check if the user exists
if user_id in users:
# Update the email from the shared variable
users[user_id]['email'] = temporary_email_storage
print(f"Email for user {user_id} changed to {temporary_email_storage}")
return f"Email changed to {temporary_email_storage} for user {user_id}", 200
else:
return "User not found", 404
if __name__ == '__main__':
app.run(debug=True, threaded=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment