Search code examples
javapythonpy4j

PY4J callback server error


I'm trying to run the example for the callback server in the PY4J website here

But I'm getting the following exception: "py4j.protocol.Py4JNetworkError: An error occurred while trying to start the callback server"

This is the code:

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();
    }

}

Interface:

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()

As I mentioned, I'm getting this exception

py4j.protocol.Py4JNetworkError: An error occurred while trying to start the callback server.

This is the stack trace:

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]

Solution

  • I found out what was the problem.

    When I'm using Sublime Text to run the Python script and when the build was complete the process that uses the port (25334 in this case) was still running so the port was in use when I tried to run the script again.

    Thanks anyway.