Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created April 27, 2018 16:13
Show Gist options
  • Save peterbe/546e782b5ae51afb22ab0eed9b3ddab1 to your computer and use it in GitHub Desktop.
Save peterbe/546e782b5ae51afb22ab0eed9b3ddab1 to your computer and use it in GitHub Desktop.
import time
import psycopg2
import concurrent.futures
def first():
conn = psycopg2.connect("dbname=buildhub_copy")
with conn.cursor() as cur:
cur.execute("""
delete from records where parent_id='peter'
""")
timestamp = 1523875992003
cur.execute("""
insert into records (id, parent_id, collection_id, data, last_modified, deleted)
values ('firefox_beta_58-0b15rc1_linux-i686_cs2', 'peter', 'record', ('{}')::JSONB,
from_epoch(%(timestamp)s),
FALSE)
""", {'timestamp': timestamp})
conn.commit()
def read_or_mess(i):
# print('!', i)
conn = psycopg2.connect("dbname=buildhub_copy")
# If you COMMENT OUT this thing, the output will be:
#
# 1) RESULT: Deleted!
# 0) COUNT: 1 #RECORDS: 0
#
# I.e. Wrong! The count doesn't match the number of records.
#
if i == 0:
conn.set_session(isolation_level='REPEATABLE READ')
with conn.cursor() as cur:
if i == 0:
cur.execute("""
select count(*) from records
WHERE parent_id='peter'
""")
count_total, = cur.fetchone()
if i == 0:
time.sleep(1)
cur.execute("""
select id from records
WHERE parent_id='peter'
""")
records = cur.fetchmany()
conn.commit()
return (count_total, records)
else:
cur.execute("""
delete from records where parent_id='peter'
""")
conn.commit()
return "Deleted!"
first()
time.sleep(1)
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_url = {executor.submit(read_or_mess, i): i for i in range(2)}
for future in concurrent.futures.as_completed(future_to_url):
i = future_to_url[future]
result = future.result()
if i == 0:
print('{}) COUNT: {}\t#RECORDS: {}'.format(i, result[0], len(result[1])))
else:
print("{}) RESULT: {}".format(i, result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment