django run websocket client in background

So when I run the WebSocket client I can no longer reach the Django app like I can’t connect to 127.0.0.1:8000, here is the WebSocket file:

import websocket, json, pprint
from datetime import datetime


socket = "wss://stream.binance.com:9443/ws/manausdt@kline_1m"


def on_message(ws, msg):
    message = json.loads(msg)
    messageData = message['k']

    if messageData['x']:
        # get_data(messageData)
        timestamp = float(messageData['t'])
        symbol = messageData['s']
        interval = messageData['i']
        closeprice = float(messageData['c'])
        openprice = float(messageData['o'])
        highprice = float(messageData['h'])
        lowprice = float(messageData['l'])
        datetime_ob = datetime.fromtimestamp(timestamp / 1000)

        data = {'timestamp': timestamp, 'symbol': symbol, 'interval': interval, 'closeprice': closeprice,
                'openprice': openprice, 'highprice': highprice, 'lowprice': lowprice, 'datetime_ob': datetime_ob}

        print(data)


def on_open(ws):
    print('Connected!')


def on_error(wsapp, err):
    print("Got a an error: ", err)


def runwss():
    stream = websocket.WebSocketApp(socket, on_error=on_error, on_open=on_open, on_message=on_message)
    stream.run_forever()


runwss()

And I inited it in apps file:

from django.apps import AppConfig


class ServiceGetDataConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'service_get_data'

    def ready(self):
        import service_get_data.wss

And as I said I can’t reach 127.0.0.1:8000 anymore so what shall I do in this case?

Answer

I searched online and ended up with using Threading module:

thread = threading.Thread(target=runwss)
thread.start()