惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
博客园 - 【当耐特】
I
InfoQ
腾讯CDC
人人都是产品经理
人人都是产品经理
H
Help Net Security
Y
Y Combinator Blog
B
Blog
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 叶小钗
D
Docker
博客园 - 聂微东
B
Blog RSS Feed
G
Google Developers Blog

blag

SQLite prefixes its temp files with `etilqs_` - blag Setsum - order agnostic, additive, subtractive checksum - blag Oldest recorded transaction - blag Replacing a cache service with a database - blag SQLite commits are not durable under default settings - blag PSA: SQLite WAL checksums fail silently and may lose data - blag Rickrolling Turso DB (SQLite rewrite in Rust) - blag Collection of insane and fun facts about SQLite - blag How bloom filters made SQLite 10x faster - blag In search of a faster SQLite - blag Galloping Search - blag Building a distributed log using S3 (under 150 lines of Go) - blag Zero Disk Architecture - blag PSA: Most databases do not do checksums by default - blag PSA: SQLite does not do checksums - blag Disaggregated Storage - a brief introduction - blag Why does SQLite (in production) have such a bad rep? - blag SQLite Slaps - blag Now - blag Learning C - blag Snapshot Testing - blag Win: contribution to libSQL (SQLite) codebase - blag Errata in Hekaton MVCC paper - blag Internet is wholesome: MVCC edition - blag It is becoming difficult for me to be productive in Python - blag MongoDB secondary only index - blag Introducing CaskDB – a project to teach you writing a key-value store - blag Recurse Center: Winter Break - blag Recurse Center Day 24: Hacking Go compiler to add a new keyword - blag Recurse Center Day 20: Django v4 upgrade (from v1) - blag
Catching SIGTERM in Python - blag
2016-02-20 · via blag

I needed a very simple SIGTERM handler in a script I was working on. It is very simple to do so in Python, add a handler method and ‘register’ it.

First define a handler method:

def sigterm_handler(signal, frame):
    # this method defines the handler i.e. what to do
    # when you receive a SIGTERM
    pass

And register the handler:

# Register the handler and let the process know that 
# there is a handler for SIGTERM
signal.signal(signal.SIGTERM, sigterm_handler) 

Here’s a simple script, if it receives kill then it saves current state and exits gracefully. Here’s a full working code:

import signal
import sys
import time


def sigterm_handler(signal, frame):
    # save the state here or do whatever you want
    print('booyah! bye bye')
    sys.exit(0)

signal.signal(signal.SIGTERM, sigterm_handler)


def main():
    for i in range(100):
        print(i)
        time.sleep(i)


if __name__ == '__main__':
    main()

Run the above script and send KILL, like

$ kill <PID here>

it will print 'booyah! bye bye' in terminal.

References

Python 3 docs on signal - link