Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Automatically restart runtime #19

Merged
merged 2 commits into from
Jul 4, 2022

Conversation

BrianHenryIE
Copy link
Contributor

There's an easy way to restart the runtime without the arduous task of clicking the menu! Found on StackOverflow.

import os
os.kill(os.getpid(), 9)

I also added some comments to explain the benign error messages that appear.

I'm not sure where to PR the Colab notebook itself.

There's an easy way to restart the runtime without the arduous task of clicking the menu! [Found on StackOverflow](https://stackoverflow.com/a/53165687/336146).

```
import os
os.kill(os.getpid(), 9)
```

I also added some comments to explain the benign error messages that appear.

I'm not sure where to PR the Colab notebook itself.
Undo unintentional line replacement
@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

That’s a great idea! I tried auto-crashing before the initial Swift-Colab release circa January 2021, but it was generally unreliable. My method called into Python code inside install_swift.sh, and I think it sometimes broke the runtime. But you seem to have found something that fixed the problem. I’ll look into it tomorrow and get that into the README ASAP!

I'm not sure where to PR the Colab notebook itself.

I would like to prevent this Python code from being automatically exposed to the user’s notebook. One option is to hide it in a special Markdown cell, but that requires clicking two cells. Some people won’t think to click the second cell. I would also have to change several test notebooks. Last but not least, it’s Python code - no way, we use Swift ;)

However, adding that as a tip on the README is a perfect idea. I can take it from here, because I have some very strict conventions for style, organization of information, etc. There is a whole Python mode to Swift-Colab, so this could serve a second purpose of auto-restarting the runtime there.

Thanks for the contribution!

@BrianHenryIE
Copy link
Contributor Author

install_swift.sh can also remove:

runtime=`cat "/opt/swift/runtime"`
runtime=$(echo $runtime | tr '[:upper:]' '[:lower:]')

if [[ $runtime == "swift" ]]; then
  echo '=== ------------------------------------------------------------------------ ===
=== Swift-Colab overwrote the Python kernel with Swift, but Colab is still   ===
=== in Python mode. To enter Swift mode, go to Runtime > Restart runtime.    ===
=== ------------------------------------------------------------------------ ==='
fi

when/if the changes are made to the notebook.

@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

This would be ideal. It involves a process’s PID and the number 9, so it should be related to your Python code.

https://stackoverflow.com/questions/2775009/kill-a-process-in-bash#2775021

@BrianHenryIE
Copy link
Contributor Author

Last but not least, it’s Python code - no way, we use Swift ;)
...
I would like to prevent this Python code from being automatically exposed to the user’s notebook.

I agree, but this is the special case of relegating Python in favour of Swift! I think 99% of people aren't going to read the first cell, or will appreciate that it is the cell that installs Swift and changes the runtime. The UX benefit is real.

@BrianHenryIE
Copy link
Contributor Author

There is a whole Python mode to Swift-Colab, so this could serve a second purpose of auto-restarting the runtime there.

I'm not sure exactly what you mean here. My two lines are really before Swift-Colab itself kicks in. Where you say "Python mode", I thought all Python from that point on is PythonKit. (bear in mind I'm still just getting familiar with this!)

This would be ideal. It involves a process’s PID and the number 9, so it should be related to your Python code.

It's the same.

pskill -9 ... is like Force Quit and pskill -15 ... is regular quit. Then the Python in the PR, os.kill(os.getpid(), 9) is a Python call to those same functions.

@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

I'm not sure exactly what you mean here. My two lines are really before Swift-Colab itself kicks in. Where you say "Python mode", I thought all Python from that point on is PythonKit. (bear in mind I'm still just getting familiar with this!)

From README.md:

Screen Shot 2022-07-03 at 10 45 00 PM

Best explanation I have: https://forums.fast.ai/t/python-textfield-output-not-working/51000/14

Work in progress, not yet legible: LanguageModes.md

From ColabSupportHistory.md:

Screen Shot 2022-07-03 at 10 48 38 PM

@philipturner
Copy link
Owner

I think 99% of people aren't going to read the first cell, or will appreciate that it is the cell that installs Swift and changes the runtime. The UX benefit is real.

That's true. We could condense the two lines of Python code using a semicolon. But if we can reduce the first cell from 4 3 to 2 lines of code by killing the process in install_swift.sh, it's even better.

@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

I'm experimenting with various ways of doing this. I would have to search for the parent to the Bash process. I could kill all Python 3 processes, but that seems excessive and could have unintentional consequences. I have a solution for finding the right parent process, though.

Screen Shot 2022-07-03 at 11 35 19 PM

However, I need to prevent the flashing cursor ANSI sequence from appearing. Hopefully I will not have to revert to Python.


Perhaps I can call Python code from within Bash, given that I know the true parent process's PID. That might avoid the flashing ANSI sequence.

@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

Python also creates a flashing ANSI sequence, if there was previously output. Maybe I need to send a clear sequence to stdout.


This works:

!echo "output"
import os
import sys
sys.stdout.flush()
os.kill(os.getpid(), 9)

I think I have a solution. Compress the code above onto one line: import os; import sys; sys.stdout.flush(); os.kill(os.getpid(), 9). Then, pad it with ~150 lines of whitespace so it isn't visible in the editor. Give a very good explanation for why I'm doing this in the documentation.

Problem solved. "The UX benefit is real" and there's no Python code in the notebook. To be more precise, it's physically outside the notebook.


Nevermind. There's an indentation error in the Python interpreter. I'll figure out what to do tomorrow. If there's no solution that satisfies my two requirements above, it will just be strongly recommended advice in the README.

@philipturner philipturner merged commit 2666943 into philipturner:main Jul 4, 2022
@philipturner
Copy link
Owner

philipturner commented Jul 4, 2022

Screen Shot 2022-07-04 at 11 16 30 AM

I often change the README on a whim, so the screenshot above may not reflect its current state.

@philipturner
Copy link
Owner

I added some cool text colorization to the loading message, so I can omit the third line from the first code cell. The line is still present in the README for clarity. The text colorization is coming in the next minor release of Swift-Colab.

Screen Shot 2022-08-11 at 11 59 07 AM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants