Reputation: 61
I've used startup scripts on Google Cloud Compute Instances:
setsid python home/junaid_athar/pull.py
And I can run the following script on the VM without issue when logged in at the root directory:
setsid python3 home/junaid_athar/btfx.py
however, when I add setsid python3 home/junaid_athar/btfx.py to the startup-script it throws an error saying:
ImportError: cannot import name 'opentype'
The same script runs fine when I'm logged in, but not when I run it as a startup-script, why and how do I resolve it?
Update: I'm pretty new to programming, and hack away. Here's the script:
import logging
import time
import sys
import json
from btfxwss import BtfxWss
from google.cloud import bigquery
log = logging.getLogger(__name__)
fh = logging.FileHandler('/home/junaid_athar/test.log')
fh.setLevel(logging.CRITICAL)
sh = logging.StreamHandler(sys.stdout)
sh.setLevel(logging.CRITICAL)
log.addHandler(sh)
log.addHandler(fh)
logging.basicConfig(level=logging.DEBUG, handlers=[fh, sh])
def stream_data(dataset_id, table_id, json_data):
bigquery_client = bigquery.Client()
dataset_ref = bigquery_client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
data = json.loads(json_data)
# Get the table from the API so that the schema is available.
table = bigquery_client.get_table(table_ref)
rows = [data]
errors = bigquery_client.create_rows(table, rows)
wss=BtfxWss()
wss.start()
while not wss.conn.connected.is_set():
time.sleep(2)
# Subscribe to some channels
wss.subscribe_to_trades('BTCUSD')
# Do something else
t = time.time()
while time.time() - t < 5:
pass
# Accessing data stored in BtfxWss:
trades_q = wss.trades('BTCUSD') # returns a Queue object for the pair.
while True:
while not trades_q.empty():
item=trades_q.get()
if item[0][0]=='te':
json_data={'SEQ':item[0][0], 'ID':item[0][1][0], 'TIMESTAMP':int(str(item[0][1][1])[:10]) , 'PRICE':item[0][1][3], 'AMOUNT':item[0][1][2], 'UNIQUE_TS':item[0][1][1], 'SOURCE':'bitfinex'}
stream_data('gdax','btfxwss', json.dumps(json_data))
# Unsubscribing from channels:
wss.unsubscribe_from_trades('BTCUSD')
# Shutting down the client:
wss.stop()
I'm running it on a Standard 1-CPU 3.75mem machine. (Debian GNU/Linux 9 (stretch)).
I THINK the problem is with the install directory of python3 & modules and the difference between how start-up scripts are ran vs being logged into the machine-- how do I troubleshoot that?
Upvotes: 1
Views: 1076
Reputation: 61
Figured out what was going on. Startup scripts are run as the (on the?) root. I added -u username
to the start of the startup script, and it ran as though I were SSH'ed into the server. All is good, thanks all for your help!
Upvotes: 5