PY4J回调服务器错误

时间:2014-09-28 11:01:47

标签: java python py4j

我试图在PY4J网站上运行回调服务器的示例 here

但我收到以下异常: " py4j.protocol.Py4JNetworkError:尝试启动回调服务器时发生错误"

这是代码:

爪哇:

package py4j.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import py4j.GatewayServer;

public class OperatorExample {

    // To prevent integer overflow
    private final static int MAX = 1000;

    public List<Integer> randomBinaryOperator(Operator op) {
            Random random = new Random();
            List<Integer> numbers = new ArrayList<Integer>();
            numbers.add(random.nextInt(MAX));
            numbers.add(random.nextInt(MAX));
            numbers.add(op.doOperation(numbers.get(0), numbers.get(1)));
            return numbers;
    }

    public List<Integer> randomTernaryOperator(Operator op) {
            Random random = new Random();
            List<Integer> numbers = new ArrayList<Integer>();
            numbers.add(random.nextInt(MAX));
            numbers.add(random.nextInt(MAX));
            numbers.add(random.nextInt(MAX));
            numbers.add(op.doOperation(numbers.get(0), numbers.get(1), numbers.get(2)));
            return numbers;
    }

    public static void main(String[] args) {
            GatewayServer server = new GatewayServer(new OperatorExample());
            server.start();
    }

}

接口:

package py4j.examples;

public interface Operator {

    public int doOperation(int i, int j);

    public int doOperation(int i, int j, int k);

}

的Python:

from py4j.java_gateway import JavaGateway

class Addition(object):
    def doOperation(self, i, j, k = None):
        if k == None:
             return i + j
        else:
             return i + j + k

    class Java:
        implements = ['py4j.examples.Operator']

if __name__ == '__main__':
    gateway = JavaGateway(start_callback_server=True)
    operator = Addition()
    numbers = gateway.entry_point.randomBinaryOperator(operator)
    print(numbers)
    numbers = gateway.entry_point.randomTernaryOperator(operator)
    print(numbers)
    gateway.shutdown()

正如我所提到的,我得到了这个例外

py4j.protocol.Py4JNetworkError:尝试启动回调服务器时出错。

这是堆栈跟踪:

Traceback (most recent call last):
  File "/home/amir/Python code/callback_example.py", line 14, in <module>
    gateway = JavaGateway(start_callback_server=True)
  File "/usr/local/lib/python2.7/dist-packages/py4j-0.8.2.1-py2.7.egg/py4j/java_gateway.py", line 851, in __init__
    self._start_callback_server(python_proxy_port)
  File "/usr/local/lib/python2.7/dist-packages/py4j-0.8.2.1-py2.7.egg/py4j/java_gateway.py", line 867, in _start_callback_server
    self._callback_server.start()
  File "/usr/local/lib/python2.7/dist-packages/py4j-0.8.2.1-py2.7.egg/py4j/java_gateway.py", line 1091, in start
    raise Py4JNetworkError(msg)
py4j.protocol.Py4JNetworkError: An error occurred while trying to start the callback server
[Finished in 0.5s with exit code 1]

1 个答案:

答案 0 :(得分:1)

我发现了问题所在。

当我使用Sublime Text运行Python脚本时,当构建完成时,使用该端口的进程(本例中为25334)仍在运行,因此当我尝试再次运行脚本时端口正在使用

非常感谢。