KxSystems / pyq

PyQ — Python for kdb+
http://code.kx.com/q/interfaces
Apache License 2.0
190 stars 49 forks source link

Socket stale when opening port in PyQ #3

Closed sitsang closed 7 years ago

sitsang commented 7 years ago

KDB+/Q supports socket connection natively. But when activating the socket listener in Q, it will not response when inside PyQ.

Server Side:

>>> pyq.q("\p 12345")
k('::')

Client Side:

>>> import pyq
>>> pyq.q("hopen `::12345")

The hopen command will wait for a long time, it seems the server side isn't responding.

abalkin commented 7 years ago

This is expected behavior. When you run pyq, q event loop is blocked in a call to Python's main loop. To avoid this, you should call python from a regular q session. For example:

Server side:

$ q
KDB+ 3.4 2016.06.14 ..
q)p)from pyq import q
q)p)def f(): print 'Hello';return q('42') # make sure to return a K object
q)p)q.f = f   # export f to q (make sure not to delete f in python)
q)f()  / check that we can call f locally
Hello
42
q)\p 1024  /  now open the communication port

Client side:

$ q
KDB+ 3.4 2016.06.14 ..
q)`::1024 "f()"
42

and 'Hello' is printed on the server side.

If you don't want to prefix every line with p) you can put your python code in a file with .p extension and load it from q

$ cat f.p
from pyq import q
def f():
    print 'Hello'
    return q('42')
q.f = f
$ q
KDB+ 3.4 2016.06.14 ..
q)\l f.p
q)f()
Hello
42
sitsang commented 7 years ago

Actually, I am using PyQ inside Apache Spark via pyspark, so the server's entry point must be from python.

Can you show me where the event loop is blocked in the python's main loop?

abalkin commented 7 years ago

Can you show me where the event loop is blocked in the python's main loop?

At this line in python.q. Note that the pyq command is more or less q python.q.