Threading module not working on Ubuntu .

Mitusha_Rani
Hi,
My system specifications are as follow:
active environment : base
active env location : /home/mitusha/anaconda3
shell level : 1
user config file : /home/mitusha/.condarc
populated config files :
conda version : 4.5.11
conda-build version : 3.15.1
python version : 3.7.0.final.0
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/4.15.0-42-generic ubuntu/16.04 glibc/2.23
platform : linux-64
I have been trying to get ticker-data through multithreading . The ticker data comes in fine as long as threaded is false. But the moment I set threaded to false, the ticks stop coming. Could you please help
  • rakeshr
    @Mitusha_Rani
    Use kws.connect(threaded=True), if you want to run external multi-thread on WebSocket.
    Can you paste your WebSocket code here?
  • naz
    @rakeshr

    To recreate the issue, you can run the standard sample code provided on https://github.com/zerodhatech/pykiteconnect with ws.connect(threaded=True) (also provided below).

    1) open a shell terminal on ubuntu 16 (with latest Anaconda installation + python version : 3.7.0.final.0 + kiteconnect=3.7.6)
    2) run - "python test_init.py" (provided below) and put in required details
    3) this will fail and no websocket will be connected.

    If you run it in the ipython/python terminal or spyder, this passes.
    Our problem is that we are running it on AWS ubuntu server and need the ability to just run "python test_init.py" command on the terminal, which surprisingly is not working.

    # test_init.py
    import logging
    from kiteconnect import KiteConnect
    logging.basicConfig(level=logging.DEBUG)

    kite = KiteConnect(api_key="xxxxxxxxxxxxxxx")

    # Redirect the user to the login url obtained
    # from kite.login_url(), and receive the request_token
    # from the registered redirect url after the login flow.
    # Once you have the request_token, obtain the access_token
    # as follows.
    print ("login url:", kite.login_url())
    request_token = input("\nPlease manually type request_token : ")
    #data = kite.generate_session("request_token_here", api_secret="your_secret")
    data = kite.generate_session(request_token, api_secret="xxxxxxxxxxxxxxxxxxx")
    kite.set_access_token(data["access_token"])

    import logging
    from kiteconnect import KiteTicker
    logging.basicConfig(level=logging.DEBUG)

    # Initialise
    #kws = KiteTicker("your_api_key", data["access_token"])
    kws = KiteTicker("xxxxxxxxxxxxxxxx", data["access_token"])
    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    print("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([738561, 5633])

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [738561])

    def on_close(ws, code, reason):
    # On connection close stop the main loop
    # Reconnection will not happen after executing `ws.stop()`
    #ws.stop()
    pass

    # Assign the callbacks.
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect(threaded=True)
  • Mitusha_Rani
    Hi @rakeshr

    I am facing the exact same issue as mentioned in the above post (chec @naz post). Let me know if you are not able to recreate the issue.
    Also, the problem comes when I try to run it through ubuntu terminal/shell using "python script.py "
  • zartimus
    zartimus edited December 2018
    @naz @Mitusha_Rani
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)


    kws = KiteTicker("<API-KEY>", "<ACCESS-TOKEN>", debug=True)


    def on_ticks(ws, ticks):
    print("Ticks: {}".format(ticks))


    def on_connect(ws, response):
    ws.subscribe([738561, 5633])

    ws.set_mode(ws.MODE_FULL, [738561])


    def on_close(ws, code, reason):
    pass

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    kws.connect(threaded=True)
    kws.connect(threaded=True) basically creates a thread inside the python process. But your process doesn't wait till the thread joins. And this is the expected behaviour from pykiteconnect library.

    Now if you do `kws.connect()` instead of `kws.connect(threaded=True)`, it wont create the thread. Ticker keeps running in the main thread.

    Where do you use threaded ticker?
    Useful in instances where user writes some logic and merely depends on data from ticker.

    eg.
    import logging
    from flask import Flask, jsonify
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)


    kws = KiteTicker("<API-KEY>", "<ACCESS-TOKEN>", debug=True)
    app = Flask(__name__)
    all_ticks = []


    def on_ticks(ws, ticks):
    global all_ticks
    print("Ticks: {}".format(ticks))
    all_ticks.extend(ticks)


    def on_connect(ws, response):
    ws.subscribe([738561, 5633])

    ws.set_mode(ws.MODE_FULL, [738561])


    def on_close(ws, code, reason):
    pass

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close

    kws.connect(threaded=True)


    @app.route("/ticks")
    def ticks():
    return jsonify(data=all_ticks)


    if __name__ == "__main__":
    app.run(port=7000)
    Here i don't want ticker to run in main thread because webserver runs on main thread and blocks it. So, i ll just delegate ticker to a daemon thread and adds ticks to a global list which is accessible by webserver.
Sign In or Register to comment.