Last active
September 22, 2016 04:21
-
-
Save udoprog/a3b32440ab9d01f2707c29f62dc3fba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
from gcloud.exceptions import Conflict | |
from gcloud import datastore | |
import random | |
import traceback | |
import os | |
import uuid | |
import time | |
import sys | |
if __name__ == "__main__": | |
project = sys.argv[1] | |
id = str(uuid.uuid4()) | |
c = datastore.Client(project) | |
def f(n): | |
attempts = 0 | |
start = time.time() | |
time.sleep(n * 0.1) | |
while True: | |
try: | |
with c.transaction(): | |
entity = c.get(c.key('Test', id)) | |
if entity is None: | |
entity = datastore.Entity(key=c.key('Test', id)) | |
try: | |
visitors = entity['visitors'] | |
except KeyError: | |
entity['visitors'] = visitors = [] | |
visitors.append(int(n)) | |
c.put(entity) | |
except Conflict as e: | |
attempts += 1 | |
time.sleep(random.random() * 0.1) | |
continue | |
break | |
return (n, time.time() - start, attempts, visitors) | |
pool = Pool(processes=16) | |
results = pool.map(f, range(16)) | |
print(id) | |
for (n, diff, attempts, visitors) in sorted(results, key=lambda e: len(e[3])): | |
print("{:02}: (took {:.2f}, attempts: {}): {}".format(n, diff, attempts, visitors)) |
Author
udoprog
commented
Sep 21, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment