gpt4 book ai didi

java - PY4J回调服务器错误

转载 作者:行者123 更新时间:2023-12-01 12:29:12 25 4
gpt4 key购买 nike

我正在尝试运行 PY4J 网站中回调服务器的示例 here

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

这是代码:

Java:

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]

最佳答案

我发现了问题所在。

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

无论如何,谢谢。

关于java - PY4J回调服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26084212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com