Websocket thread deadlocked python

I’m just wondering if anyone understands why this websocket thread is deadlocked. Everything works propely on it’s own, but after websocket_thread is started the main thread won’t continue. Thanks.

auth.py

import time, hmac, hashlib

API_KEY = ''
SECRET = ''

timestamp = str(int(time.time()))

# for websocket
def sign_message(message):
    message = hmac.new(SECRET.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
    return message

create_websocket.py

import json, websocket
from threading import Thread
from auth import *

import time

def create_websocket(market):
    channel = 'ticker'
    subscribe_msg = {
        'type': 'subscribe',
        'product_ids': [
            market
            ],
        'channel': 'ticker',
        'api_key': API_KEY,
        'timestamp': timestamp,
        'signature': sign_message(timestamp + channel + market)
    }
    subscribe_msg = json.dumps(subscribe_msg)

    ws = websocket.create_connection('wss://advanced-trade-ws.coinbase.com')
    ws.send(subscribe_msg)

    while True:
        message = json.loads(ws.recv())
        if 'tickers' in message['events'][0]:
            current_price = message['events'][0]['tickers'][0]['price']
            print(current_price)

websocket_thread = Thread(target=create_websocket('BTC-USDT'))
websocket_thread.start()

# main thread
for i in range(100):
    print(i)
    time.sleep(1)

Hello @Mike1! Thank you for taking an interest in trying out Advanced Trade Websocket feed. For the details regarding your concern, we will check on this for you with our team to see how we can best assist. We’ll get back to you once we have more information. Keep in touch!

1 Like

Thanks @ereeca15 I appreciate the help

edit -
I solved the problem by removing the market parameter from the thread target and adding the args parameter. Anyway, for reference here is the updated code:

import json, websocket
from threading import Thread
from auth import *

import time

def create_websocket(market):
    channel = 'ticker'
    subscribe_msg = {
        'type': 'subscribe',
        'product_ids': [
            market
            ],
        'channel': 'ticker',
        'api_key': API_KEY,
        'timestamp': timestamp,
        'signature': sign_message(timestamp + channel + market)
    }
    subscribe_msg = json.dumps(subscribe_msg)

    ws = websocket.create_connection('wss://advanced-trade-ws.coinbase.com')
    ws.send(subscribe_msg)

    while True:
        message = json.loads(ws.recv())
        if 'tickers' in message['events'][0]:
            current_price = message['events'][0]['tickers'][0]['price']
            print(current_price)

Thread(target=create_websocket, args=('BTC-USDT',)).start()


# main thread
for i in range(100):
    time.sleep(1)
    print('main thread')
1 Like

Hi @Mike1! We are glad it all worked for you, and thank you for sharing your code. Please reach out in the future if you need anything else. Have a good day!

1 Like