How to run Anki-Qt script without requiring a breakpoint?

If I run the toy script below as shown:

import sys
import aqt

app = aqt._run(argv=['/usr/bin/anki', '--profile=test'], exec=False)
# breakpoint()

…I get the following output, which is not right:

$ python3 /tmp/

If I uncomment the commented statement, however, and re-run the modified script, I get the correct output (eventually):

$ python3 /tmp/
> /tmp/<module>()
-> print(repr(
(Pdb) c
<anki.collection._Collection object at 0x7f32ec1417b8>

I would like to avoid the need for the breakpoint() statement (and for having to hit c whenever I want to run such code).

My guess is that, when the breakpoint() statement is commented out, the print statement happens before has been fully initialized.

(I tried replacing the breakpoint() statement with time.sleep(1), but when I run the script with this modification, it hangs before ever printing any output.)

Q: How can I modify the toy script above so that, by the time the print statement executes, has its correct value?


It seems that calling aqt._run(*args, exec=False) returns a QApplication object – but without starting its event-loop. To manually process pending events, you could therefore try calling app.processEvents().

From the comments, it appears the exact solution is as follows:

while is None: