"Keyboard Interrupt System Exit"
Sometimes you run you python code in tmux like this:
- ~$ ssh [email protected]
- (insert password)
- ~# tmux attach
- (select some tmux window)
- ~# python3 your_script.py
More useful example is Machine Learning using python
You have many little works in your data processing. For each little work you have some *.py file. It is useful, really?
Of cause in production you write "big-big system"... But in R&D tmux using is more useful...
Most useful method of stop python script is send CTRL+C signal.
But in this case you have problems in data integrity...
For example you whant do this:
sql="""
insert into mytable values ('bat', 'lala')
"""
engine.execute(sql)
channel.basic_ack(delivery_tag=method.delivery_tag)
First two lines is SQLAlchemy execute command. Second is command for ack in RabbitMQ.
Of course you want to run both commands or not to run both. If your code insert information to database but not ack in RabbitMQ, data integrity will be breaked!
There are four type solutions of this problem:
- don't stop by Ctrl+C
- use atexit
- use some BIG-BI-I-IG framework for solving ALL problems in data integrity... Including "Ctrl+C" problem...
- became humility...
Therefore I spend a lot of time and make vary simple *.py code. I call it kisa. This means Keyboard Interrupt and System Exit.
(I use name "kisA" not "kisE",
becouse "kisa" is nickname
of Ippolit Vorobianinov (see russian novel "The Twelve Chairs" )
but kise means nothing.
ATTANTION: Don't speak kisKa,
becouse this is mean "pussy")
Kisa allows do this:
with Kisa():
sql="""
insert into mytable values ('bat', 'lala')
"""
engine.execute(sql)
channel.basic_ack(delivery_tag=method.delivery_tag)
If you send Ctrl+C, all code in with statement will be done!
ATTANTIONS
- I dont know is Kisa work in multithreads... But in in simple *.py data science workers is is not actual problem
- Kisa DOES NOT except some program, logic or environment errors! Kisa is solutions for simple using python code in console (ssh + tmux). Kisa allows you send some "close-program SIGNALS" without data integrity break
you can dump git solution:
git clone https://github.com/PavelMSTU/kisa
but project is vary tiny and have only one *.py file. This is kisa.py.
You can download this file only. ;)
After you can do this:
from kisa import Kisa
...
...
with Kisa():
# some data integrity critical code
...
# another code
If user send Ctrl+C before or after with statement, script is interrupt. If user send Ctrl+C in with statement, all code will be done and after KeyboardInterrupt will be send.
###cookbook
TODO
Kisa is LGPL.
It will be boorish in another case...
You can copy, fork, use all in kisa.
This code will be write in Russia. Therefore NOTHING disclaimer. If you are stupied -- this is YOUR problem. In Russia, I dont need write some bullshit text for protect my liability... Ha-ha-ha!...