我正在尝试使用 Python 和 Julia 实现一个基本的客户端-服务器套接字结构,其中生产者在 Python 中,消费者在 Julia 中。
我在 Python 端的代码如下所示:
def startServer(host='127.0.0.1', port=4002):
connected = False
s = socket.socket()
s.bind((host, port))
s.listen(5)
scon, addr = s.accept()
print 'Got connection from', addr
return scon, addr
在 Julia 方面,它看起来像这样:
using PyCall
@pyimport server as sdlib
@async begin
sleep(10)
print("In the async thread\n")
s,a = sdlib.startServer("127.0.0.1",4002)
print("Server started\n")
end
print("After the async thread\n")
print("Connecting...\n")
connected = false
while !connected
try
connected = true
c = connect(4002)
print("Connected = $(connected), $(c)\n")
catch ex
print("$(ex)\n")
connected = false
sleep(1)
end
end
print("Connection established: $(c)\n")
输出如下所示:
After the async thread
Connecting...
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
connect: connection refused (ECONNREFUSED)
In the async thread
似乎正在发生的事情是,一旦 Python 侦听器启动,线程就会锁定等待连接。控制似乎永远不会传递回主线程以允许客户端连接。
感谢我能得到的任何帮助。
谢谢,拉维