与PyCall一起使用时Julia中的异步线程死锁

时间:2014-01-14 11:58:35

标签: python multithreading julia

我正在尝试使用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侦听器启动,线程就会锁定等待连接。 Control似乎永远不会传回主线程以允许客户端连接。

感谢我能得到的任何帮助。

谢谢, 拉维

1 个答案:

答案 0 :(得分:0)

我远离我的知识领域,但由于没有人在10小时内回答,我会尝试。

我认为问题在于julia没有积极的调度程序。您可以使用并发功能,但它们将在同一进程中协同安排。由于PyCall不返回或调用(julia?)睡眠,控件将不会返回到客户端。你可以试试addprocs或用-p 2开始julia,但我不确定它会有用。

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