Interrupt and Resume execution

123 views
Skip to first unread message

Rodion Gorkovenko

unread,
Jul 4, 2016, 5:10:21 PM7/4/16
to brython
Dear Friends, Hi!

Is there a way to interrupt execution of python script in brython and then resume it from that state?

The matter is I'm trying to come up with some simple game-like functionality to teach basics of python. The idea is to display game field with some character on it and ask user to control this character with simple python code like this:

forward()
turnLeft()
for i in range(3):
    forward()
pickObject()

The problem is that actions like "forward" and "turnLeft" should take some time (and probably be animated). And I do not want browser to freeze during the execution.

I suppose that if I find a way to interrupt the program (e.g. on "forward") call, then play required animation outside of the user's code, and then resume the execution - I will achieve the goal.

I also think such "interruption" could be used for simulating blocking operations so I suspect it exists in some form but I fail to find it. I read about debugging (and auto inserting trace between lines, but as I understand it works only in record mode?) Though perhaps I can fall down to recording the execution and replaying it for user...

Thank you in advance,
Rodion

P.S. Initially I tried "skulpt" interpreter for this task, because I know no better option. But after I came upon the brython I feel extremely inclined to use it for it feels significantly more "friendly" to me in many aspects and perhaps more advanced (though I do not want to say skulpt is bad - its developers I believe do great work too).

Olemis Lang

unread,
Jul 4, 2016, 6:09:06 PM7/4/16
to bry...@googlegroups.com
On 7/4/16, Rodion Gorkovenko <rodio...@gmail.com> wrote:
> Dear Friends, Hi!
>
> Is there a way to interrupt execution of python script in brython and then
> resume it from that state?
>

I'm assuming you are asking for a javascript API to do so. If this is
the case, at this moment this is not possible . Brython core is
synchronous .

> The matter is I'm trying to come up with some simple game-like
> functionality to teach basics of python. The idea is to display game field
> with some character on it and ask user to control this character with
> simple python code like this:
>
> forward()
> turnLeft()
> for i in range(3):
> forward()
> pickObject()
>
> The problem is that actions like "forward" and "turnLeft" should take some
> time (and probably be animated). And I do not want browser to freeze during
> the execution.
>

You should work around this in Python code by writing a coroutine , I guess.

> I suppose that if I find a way to interrupt the program (e.g. on "forward")
>
> call, then play required animation outside of the user's code, and then
> resume the execution - I will achieve the goal.
>
> I also think such "interruption" could be used for simulating blocking
> operations

We've discussed before the decision of implementing an async core +
AMD imports [1]_ but, AFAICT, that's a non-trivial refactoring .

[...]

.. [1] https://github.com/brython-dev/brython/issues/188

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Brython committer
http://brython.info
http://github.com/brython-dev/brython

SciPy Latin America - Cuban Ambassador
Chairman of SciPy LA 2017

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

André

unread,
Jul 4, 2016, 8:51:37 PM7/4/16
to brython


On Monday, 4 July 2016 18:10:21 UTC-3, Rodion Gorkovenko wrote:
Dear Friends, Hi!

Is there a way to interrupt execution of python script in brython and then resume it from that state?

The matter is I'm trying to come up with some simple game-like functionality to teach basics of python. The idea is to display game field with some character on it and ask user to control this character with simple python code like this:

forward()
turnLeft()
for i in range(3):
    forward()
pickObject()

Perhaps, instead of re-inventing the wheel, you could either use Reeborg's World as-is (http://reeborg.ca/reeborg.html - tutorial/documentation at http://reeborg.ca/docs/en/) or build from its (slightly messy) code.  Reeborg's World uses Brython.

 

The problem is that actions like "forward" and "turnLeft" should take some time (and probably be animated). And I do not want browser to freeze during the execution.

I suppose that if I find a way to interrupt the program (e.g. on "forward") call, then play required animation outside of the user's code, and then resume the execution - I will achieve the goal.
I have not been able to find a way to do this as such. 

I also think such "interruption" could be used for simulating blocking operations so I suspect it exists in some form but I fail to find it.
No, it does not exists within  Brython.

 
I read about debugging (and auto inserting trace between lines, but as I understand it works only in record mode?) Though perhaps I can fall down to recording the execution and replaying it for user...

That is what I have done with Reeborg's World.

André

Pierre Quentel

unread,
Jul 5, 2016, 1:48:09 AM7/5/16
to brython


Le mardi 5 juillet 2016 02:51:37 UTC+2, André a écrit :


On Monday, 4 July 2016 18:10:21 UTC-3, Rodion Gorkovenko wrote:
Dear Friends, Hi!

Is there a way to interrupt execution of python script in brython and then resume it from that state?

The matter is I'm trying to come up with some simple game-like functionality to teach basics of python. The idea is to display game field with some character on it and ask user to control this character with simple python code like this:

forward()
turnLeft()
for i in range(3):
    forward()
pickObject()

Perhaps, instead of re-inventing the wheel, you could either use Reeborg's World as-is (http://reeborg.ca/reeborg.html - tutorial/documentation at http://reeborg.ca/docs/en/) or build from its (slightly messy) code.  Reeborg's World uses Brython.

 
You can also take a look at the implementation of turtle made by Billy Earney : http://brython.info/gallery/turtle.html. The turtle module is in Lib/site-packages.

Rodion Gorkovenko

unread,
Jul 5, 2016, 3:41:52 AM7/5/16
to brython
> Is there a way to interrupt execution of python script in brython and then
> resume it from that state?
>

I'm assuming you are asking for a javascript API to do so. If this is
the case, at this moment this is not possible . Brython core is
synchronous .


Hi! Thanks for your answer!

I think I'm not necessarily about javascript API. I rather meant to ask about some synchronous way, e.g.

method called from user's code (like "forward" or "turnLeft") throws/raises some kind of exception, which is caught outside the user's code and the state of execution is somehow preserved, so that I can further call "exec(...)" in some manner which allow to continue execution (after I took care of necessary interaction with user).

This will also probably allow implementation of interactive "input()" etc.


> Perhaps, instead of re-inventing the wheel, you could either use Reeborg's World

Andre, thanks a lot for the link. I surely investigate this and probably will address to you directly with some questions :)
Looks great!

Olemis Lang

unread,
Jul 5, 2016, 11:33:46 AM7/5/16
to bry...@googlegroups.com
On 7/5/16, Rodion Gorkovenko <rodio...@gmail.com> wrote:
>
>>
>> > Is there a way to interrupt execution of python script in brython and
>> then
>> > resume it from that state?
>> >
>>
>> I'm assuming you are asking for a javascript API to do so. If this is
>> the case, at this moment this is not possible . Brython core is
>> synchronous .
>>
>>
> Hi! Thanks for your answer!
>
> I think I'm not necessarily about javascript API. I rather meant to ask
> about some synchronous way, e.g.
>
> method called from user's code (like "forward" or "turnLeft") throws/raises
>
> some kind of exception, which is caught outside the user's code and the
> state of execution is somehow preserved, so that I can further call
> "exec(...)" in some manner which allow to continue execution (after I took
> care of necessary interaction with user).

The more you explain me what you want to do , I get the feeling that
you might be in need of coroutines ...

[...]
>> Perhaps, instead of re-inventing the wheel, you could either use
> Reeborg's World
>
> Andre, thanks a lot for the link. I surely investigate this and probably
> will address to you directly with some questions :)
> Looks great!
>

... or going this way ;)

[...]
Reply all
Reply to author
Forward
0 new messages