Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The logger method in Application interface terminates python #175

Closed
OllieKampo opened this issue Nov 6, 2019 · 2 comments
Closed

The logger method in Application interface terminates python #175

OllieKampo opened this issue Nov 6, 2019 · 2 comments
Assignees
Labels
Milestone

Comments

@OllieKampo
Copy link

OllieKampo commented Nov 6, 2019

According to the clingo api documentation 5.4 for python (https://potassco.org/clingo/python-api/5.4/) you can use the method:

def logger(self, code: MessageCode, message: str) -> None:

Within the Application interface to intercept messages normally printed to standard error.
I am trying to use this, but whenever my Application class implements this method python terminates almost immediately after any calls to control.ground(parts).

For example:

def logger(self, code: clingo.MessageCode, message: str) -> None:
        self._data.get_logger().add_log("TEST", message)

def main(self, control: clingo.Control, files: List[str]):
        self.__build_program(control)
        start_time: float = time.clock()
        self._data.get_logger().add_log("ASP_SYSTEM", "Grounder invoked")
        if self.is_planning_mode:
            control.ground([("base", []), ("planning_module", []), ("recorded_history", [])])
        else:
            control.ground([("base", []), ("diagnostics_module", []), ("recorded_history", [])])
        self._data.get_logger().add_log("ASP_SYSTEM", "Grounder returned (duration {0} seconds)".format(time.clock() - start_time))
        start_time = time.clock()
        self._data.get_logger().add_log("ASP_SYSTEM", "Solver invoked")
        handle: clingo.SolveHandle
        with control.solve(on_model = self.on_model, on_finish = self.on_finish, async_ = True) as handle:
            while not handle.wait(0.1):
                for cursor in '\\|/-':
                    sys.stdout.write('\r{0}'.format(cursor))
                    sys.stdout.flush()
            self._data.get_logger().add_log("ASP_SYSTEM",
                                            "Solver returned {0} with {1} (duration {2} seconds)".format(handle.get(),
                                                                                                         "OPTIMUM FOUND" if handle.get().exhausted else "OPTIMUM NOT FOUND",
                                                                                                         time.clock() - start_time))

def __build_program(self, control: clingo.Control) -> None:
        builder: clingo.ProgramBuilder
        with control.builder() as builder:
            part: ast.AST
            for part in self.__program:
                builder.add(part)

Will print [23:11:31] ASP_SYSTEM: Grounder invoked and then immediately terminate python.
I have tried surrounding the grounder call with a try/except block but this does not work.
I cannot understand the problem at all, it does not seem to throw any sort of error or exception, it just terminates python...

Note that if I comment out the logger method it runs exactly as expected, no errors.

Any ideas?

rkaminsk added a commit that referenced this issue Nov 7, 2019
Since the logger of the application is called during, the GIL has to be
acquired.
@rkaminsk rkaminsk self-assigned this Nov 7, 2019
@rkaminsk rkaminsk added the bug label Nov 7, 2019
@rkaminsk rkaminsk added this to the v5.4.1 milestone Nov 7, 2019
@rkaminsk
Copy link
Member

rkaminsk commented Nov 7, 2019

Thanks for reporting. The problem is fixed in the wip branch. See the linked commit. The next release will still take a little because there are some further things I want to address.

@OllieKampo
Copy link
Author

Excellent, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants