Class P4.PyKeepAlive

Note

This release of P4Python does not support p4.setbreak() method during parallel sync operations. This feature will be updated in a future release once the relevant bug(s) in P4API are fixed. It is recommended to use p4.run_sync("--parallel=threads=0") on a Helix Core Server with parallel sync enabled.

Description

The only method of the PyKeepAlive class, isAlive(), is used in applications to request the current command to be terminated by disconnecting. To terminate a connection, an object of the PyKeepAlive class should be passed to p4.setbreak() method to establish a callback that is called every 0.5 seconds during command execution.

Instance Attributes

None.

Class Methods

None.

Instance Methods

isAlive() -> int

The isAlive() method is a public method that can be customized as per the user's requirements. By default, it returns 1. Users can override this method to implement specific logic for deciding whether to terminate the current command by returning 0 or 1, respectively.

For example, the following code implements a custom isAlive() method that can be called three times before returning 0 and terminating the connection. If the call to run the changes command takes less than 1.5 seconds to complete on the server side, the program outputs the list of changes. If the call to run the changes command takes more than 1.5 seconds, the connection is interrupted.

import P4

# subclass KeepAlive to implement a customized IsAlive function.
			
class MyKeepAlive(P4.PyKeepAlive):
    def __init__(self):
        P4.PyKeepAlive.__init__(self)
        self.counter = 0

    # Set up the interrupt callback. After being called 3 times,
    # interrupt 3 times, interrupt the current server operation.
    def isAlive(self):
        self.counter += 1
        if self.counter > 3:
            return 0
        return 1

# Now test the callback
p4 = P4.P4()
p4.connect()

kaObj=MyKeepAlive()
p4.setbreak(kaObj)     # SetBreak must happen after the p4.connect()
p4.run("changes")
p4.disconnect()