1
我试图用Python和Julia实现一个基本的客户端服务器套接字结构,其中生产者在Python中,消费者在Julia中。Julia异步线程死锁与PyCall一起使用
我在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
在朱莉娅的一面,它看起来像这样:
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侦听器启动,线程就会锁定等待连接。控制似乎永远不会传回主线程以允许客户端连接。
感谢任何帮助,我可以得到这一点。
感谢, 拉维
谢谢,ivarne。我曾尝试使用-p 2,但似乎没有帮助。将仔细看看文档。 – msravi
你需要'@everywhere使用PyCall',把'@ async'改成'@spawnat 2 begin ...',并把'@ pyimport'放在spawnat块中。我只是试了一下,它工作得很好。或者使用Python的多处理模块(或者可能是asynccore)。 –