KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums PyKX PyKX could not establish an IPC connection in the Flask app

  • PyKX could not establish an IPC connection in the Flask app

    Posted by supersaiyanraj on November 14, 2023 at 12:00 am

    I’m trying to make a connection from a Flask app, but it shows a QError: .pykx.i.isw.

    I tried the same function without a Flask app, and it can make a connection.  Please find attached.

    Can you please take a look on it. 

    rocuinneagain replied 7 months, 3 weeks ago 4 Members · 10 Replies
  • 10 Replies
  • rocuinneagain

    Member
    November 14, 2023 at 12:00 am

    Hi, What version of PyKX are you using?

    .pykx.i.isw has been renamed to .pykx.util.isw since 2.0.0

  • supersaiyanraj

    Member
    November 15, 2023 at 12:00 am

    I tried in pykx 1.6.3

    pip list | grep pykx

    pykx 1.6.3

    after updated the package into 2.2.0 – It make a IPC connection, Thanks a lot.

  • ryanas

    Member
    December 5, 2023 at 12:00 am

    Hey, I have issue when I setup the flask server like you described, I managed to get the response, but also got error logs like in the image attachment. Do you know something about this?

  • rocuinneagain

    Member
    December 5, 2023 at 12:00 am

    See: PYKX_SKIP_SIGNAL_OVERWRITE on

    https://code.kx.com/pykx/2.2/user-guide/configuration.html

    *New in 2.2.1

  • ryanas

    Member
    December 5, 2023 at 12:00 am

    Thanks for your answer, ! However, I still find the same error logs even with that config enabled. Not sure if this affects the issue, but I am using Flask 3.0 and PyKX 2.2.1. I have tried using other frameworks such as Django or FastAPI, and both are running fine. So, I wonder whether this problem is specific to Flask.

  • rsedman

    Member
    December 11, 2023 at 12:00 am

    Did you manage to find a solution for this? I have the same issue!Running flask==3.0.0 and pykx==2.2.1

  • supersaiyanraj

    Member
    February 8, 2024 at 12:00 am

    Hello ,I tried the same with both pykx 2.2.0 /2.3.0 , python 3.10.9 , Flask 3.0.2, I can’t able to connect with port 5000.

    from flask import Flask 
    import pykx as kx 
    import json as j 
    app = Flask(__name__) 
    
    @app.route("/") 
    def hello_kx(): 
         import pykx as kx 
         qcon=kx.QConnection(host='localhost', port=5000) 
         tables=qcon("tables[]").py() 
         import json as j 
         print(j.dumps(ports)) 
         return tables 
    
    if __name__ == '__main__': 
         app.run(host='127.0.0.1', debug=True, port=8100)

    I got this error:

    if q(‘{.pykx.util.isw x}’, self).py():

    File “/rxds/rxds_offerings/datagen/env/lib/python3.10/site-packages/pykx/embedded_q.py”, line 226, in __call__return factory(result, False)

    File “pykx/_wrappers.pyx”, line 507, in pykx._wrappers._factory

    File “pykx/_wrappers.pyx”, line 500, in pykx._wrappers.factory

    pykx.exceptions.QError: .pykx.util.isw

    the same code I just run python without flask it return values.

    import pykx as kx 
    import json 
    
    def hello_kx(): 
         qcon=kx.QConnection(host='localhost', port=5000) 
         tables=qcon("tables[]").py() 
         import json 
         return json.dumps(tables)
    
    a=hello_kx() 
    print(a)
  • rocuinneagain

    Member
    February 9, 2024 at 12:00 am

    Or if you only use PyKX for querying then you can use in unlicensed mode

    https://code.kx.com/pykx/2.3/user-guide/advanced/modes.html#operating-in-the-absence-of-a-kx-license

    from flask import Flask 
    import os 
    os.environ['PYKX_UNLICENSED']='true' 
    
    import pykx as kx 
    import json as j 
    app = Flask(__name__) 
    
    @app.route("/") 
    def hello_kx(): 
        #import pykx as kx 
        qcon=kx.QConnection(host='localhost', port=5000) 
        tables=qcon("tables[]").py() 
        #import json as j 
        #print(j.dumps(ports)) 
        return tables 
    
    if __name__ == '__main__': 
        app.run(host='127.0.0.1', debug=True, port=8100)
  • rocuinneagain

    Member
    February 9, 2024 at 12:00 am

    You example will run if you wait and only import pykx within the app

    from flask import Flask 
    #import pykx as kx <-- remove this 
    import json as j 
    app = Flask(__name__)
    
    @app.route("/") 
    def hello_kx(): 
        import pykx as kx 
        qcon=kx.QConnection(host='localhost', port=5000) 
        tables=qcon("tables[]").py() 
        import json as j 
        print(j.dumps(ports)) 
        return tables 
    
    if __name__ == '__main__': 
        app.run(host='127.0.0.1', debug=True, port=8100)
  • rocuinneagain

    Member
    April 19, 2024 at 12:10 pm

    We added some functionality in version 2.3.0

    https://code.kx.com/pykx/2.4/release-notes/changelog.html#pykx-230

    If you enable beta features as well as pykx threading all calls into q from any python thread will be run as if they were calling from the main thread which allows python multithreaded programs to use IPC connections in licensed mode.

    You can enable this functionality like this:

    import os
    os.environ['PYKX_THREADING'] = '1'
    os.environ['PYKX_BETA_FEATURES'] = '1'
    import pykx as kx

    You will also want to ensure that kx.shutdown_thread()​ is called when the script finishes. The safest way to do this is within a try​ – finally​ block like this.

    if __name__ == '__main__':
    try:
    main()
    finally:
    kx.shutdown_thread()

    More information about this functionality and an example can be found within our documentation:

    https://code.kx.com/pykx/2.4/examples/threaded_execution/threading.html

Log in to reply.