Code Monkey home page Code Monkey logo

kisa's Introduction

kisa

"Keyboard Interrupt System Exit"

Introduction

Sometimes you run you python code in tmux like this:

  1. ~$ ssh [email protected]
  2. (insert password)
  3. ~# tmux attach
  4. (select some tmux window)
  5. ~# 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:

  1. don't stop by Ctrl+C
  2. use atexit
  3. use some BIG-BI-I-IG framework for solving ALL problems in data integrity... Including "Ctrl+C" problem...
  4. 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

  1. I dont know is Kisa work in multithreads... But in in simple *.py data science workers is is not actual problem
  2. 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

How to use

quick start

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

License

Kisa is LGPL.

It will be boorish in another case...

You can copy, fork, use all in kisa.

Disclaimer

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!...

kisa's People

Contributors

pavelmstu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.