Sometimes the best way to perform complex behavior in a callback is to put that behavior into an external script which can take the job ID or the job ID and agenda item name (the frame number) as arguments.
The following is an example of a callback that runs for every frame when the job completes. Admittedly, it may be more efficient to run it once and have the external script iterate over every frame, but this is for demonstration purposes.