Skip to content

Instantly share code, notes, and snippets.

@Celeborn2BeAlive
Created January 18, 2018 00:48
Show Gist options
  • Save Celeborn2BeAlive/30afcd35f18db4c60484eabcfdcb101e to your computer and use it in GitHub Desktop.
Save Celeborn2BeAlive/30afcd35f18db4c60484eabcfdcb101e to your computer and use it in GitHub Desktop.
Trying to mix multi-threaded flask with python-bittrex-websocket
#!/usr/bin/python
# -*- coding: utf-8 -*-
# bittrex_websocket/examples/ticker_updates.py
# Stanislav Lazarov
# Sample script to show how subscribe_to_ticker_update() works.
# Overview:
# Creates a custom ticker_updates_container dict through on_open method.
# Subscribes to N tickers to get their general information.
# When information is received, checks if the ticker is in ticker_updates_container and adds it if not.
# Disconnects when it has the information for each ticker.
from __future__ import print_function
from time import sleep
from bittrex_websocket.websocket_client import BittrexSocket
from flask import Flask, render_template, request
def main():
class MySocket(BittrexSocket):
def __init__(self):
BittrexSocket.__init__(self)
self.ticker_updates_container = {}
def on_ticker_update(self, msg):
name = msg['MarketName']
if name not in self.ticker_updates_container:
self.ticker_updates_container[name] = msg
print('Just received ticker update for {}.'.format(name))
app = Flask(__name__)
# Create the socket instance
ws = MySocket()
# Enable logging
ws.enable_log()
# Define tickers
tickers = ['BTC-ETH', 'BTC-NEO', 'BTC-ZEC', 'ETH-NEO', 'ETH-ZEC']
# Subscribe to ticker information
ws.subscribe_to_ticker_update(tickers)
while len(ws.ticker_updates_container) < len(tickers):
sleep(1)
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(threaded = True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment