2014-01-14 21 views
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侦听器启动,线程就会锁定等待连接。控制似乎永远不会传回主线程以允许客户端连接。

感谢任何帮助,我可以得到这一点。

感谢, 拉维

回答

0

我远远超出我的知识范围在这里,但在10小时内无人接听,我会作出尝试。

我认为问题在于茱莉亚没有积极的调度程序。您可以使用并发功能,但它们将在同一过程中进行协调调度。由于PyCall不返回或调用(julia?)睡眠,控件将不会返回到客户端。您可以尝试使用addprocs或使用-p 2启动julia,但我不确定这会对您有所帮助。

另请参阅http://docs.julialang.org/en/release-0.2/manual/parallel-computing/这里更好地解释。

+0

谢谢,ivarne。我曾尝试使用-p 2,但似乎没有帮助。将仔细看看文档。 – msravi

+1

你需要'@everywhere使用PyCall',把'@ async'改成'@spawnat 2 begin ...',并把'@ pyimport'放在spawnat块中。我只是试了一下,它工作得很好。或者使用Python的多处理模块(或者可能是asynccore)。 –