Skip to content

Commit e159d22

Browse files
author
William Bradley
committed
Implemented single instance handling for per_cpu.py and show_bitcoin_detail.py. I used to different implementations. One uses fcntl and one uses sockets. At present, if I use only one implementation, it makes it where only one details window can be open per tint2 session. This might be ok but what I really had in mi nd was only on script instance runs per executor.
1 parent 0b3bc5a commit e159d22

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

tint2/executors/bitcoin/show_bitcoin_detail.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111
import matplotlib.pyplot as plt
1212
plt.style.use('dark_background')
1313
import datetime
14-
import sys
15-
#The following ensures that only one instance of this program will run.
16-
try:
17-
import socket
18-
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
19-
## Create an abstract socket, by prefixing it with null.
20-
s.bind( '\0postconnect_gateway_notify_lock')
21-
except socket.error as e:
22-
error_code = e.args[0]
23-
error_string = e.args[1]
24-
print "Process already running (%d:%s ). Exiting" % ( error_code, error_string)
25-
sys.exit (0)
26-
# Get historical bitcoin price information from gdax
27-
client = gdax.PublicClient()
28-
rates = client.get_product_historic_rates('BTC-USD', granularity=60*60*24)
29-
# Modify the dates from ISO 8601 to human readable.
30-
dates = [datetime.date.fromtimestamp(element[0]) for element in rates]
31-
# Create the pandas plot.
32-
df = pd.DataFrame(rates, columns = ['date', 'low', 'high', 'open', 'close', 'volume'])
33-
df['date'] = [datetime.date.fromtimestamp(element[0]) for element in rates]
34-
df = df.head(30)
35-
df = df.sort_values('date', ascending = True)
36-
df.plot('date', 'close', kind='line', use_index=True, title='Bitcoin closing prices by date', rot=90, grid=True, figsize=[6,9])
37-
plt.show()
14+
import subprocess
15+
import os, fcntl
16+
def run_once():
17+
global fh
18+
fh=open(os.path.realpath(__file__),'r')
19+
try:
20+
fcntl.flock(fh,fcntl.LOCK_EX|fcntl.LOCK_NB)
21+
except:
22+
os._exit(0)
23+
if __name__ == "__main__":
24+
run_once()
25+
# Get historical bitcoin price information from gdax
26+
client = gdax.PublicClient()
27+
rates = client.get_product_historic_rates('BTC-USD', granularity=60*60*24)
28+
# Modify the dates from ISO 8601 to human readable.
29+
dates = [datetime.date.fromtimestamp(element[0]) for element in rates]
30+
# Create the pandas plot.
31+
df = pd.DataFrame(rates, columns = ['date', 'low', 'high', 'open', 'close', 'volume'])
32+
df['date'] = [datetime.date.fromtimestamp(element[0]) for element in rates]
33+
df = df.head(30)
34+
df = df.sort_values('date', ascending = True)
35+
df.plot('date', 'close', kind='line', use_index=True, title='Bitcoin closing prices by date', rot=90, grid=True, figsize=[6,9])
36+
plt.show()
37+

0 commit comments

Comments
 (0)