Problems using python with Editor in UE4

Hello, I met a problem when i using python with Editor in UE4.
I find we can use python command “unreal.EditorLevelLibrary.editor_play_simulate()” to play the editor and use python command “unreal.EditorLevelLibrary.editor_end_play()” to stop the play mode.
But I write these two commands in a file, for example “test.py”, and then run"py test.py", I find the editor is still in the play mode, the stop command doesn’t work.
Does anyone have suggestions?
Thank you.

I experience this as well, UE 4.27

Hello,
I guess it has to do with python working in one thread, or basically locking the process, and there is no time for editor to start when the end_play is triggered.

It works with a below snippet. Hope it helps.

import unreal
from threading import Timer

def hello():
    print("hello, world")
    unreal.EditorLevelLibrary.editor_end_play()

unreal.EditorLevelLibrary.editor_play_simulate()
t = Timer(5.0, hello)
t.start() # after 5 seconds, "hello, world" will be printed and editor will stop

Regards,
Alex.