Created
March 3, 2017 15:06
-
-
Save peterbe/585bfd8f93e6c18a22284a2a8b44bb0a 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
import threading | |
import psycopg2 | |
def updater(): | |
connection = psycopg2.connect('dbname=crontabber_exampleapp') | |
cursor = connection.cursor() | |
cursor.execute(""" | |
SELECT ongoing FROM crontabber WHERE app_name = 'bar' | |
FOR UPDATE NOWAIT | |
""") | |
cursor.execute(""" | |
UPDATE crontabber SET ongoing = now() WHERE app_name = 'bar' | |
""") | |
print("JOB CAN START!") | |
connection.commit() | |
# Use threads to simulate starting two connections virtually | |
# simultaneously. | |
threads = [ | |
threading.Thread(target=updater), | |
threading.Thread(target=updater), | |
] | |
for thread in threads: | |
thread.start() | |
for thread in threads: | |
thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment