Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / async.py
Last active November 23, 2022 01:04 — forked from phizaz/async.py
Python turn sync functions to async (and async to sync)
from functools import wraps
import asyncio
def get_event_loop():
try:
return asyncio.get_event_loop()
except RuntimeError as e:
if "There is no current event loop in thread" in str(e):
loop = asyncio.new_event_loop()
@wonderbeyond
wonderbeyond / make_data_url.py
Last active September 14, 2018 10:04 — forked from olooney/make_data_url.py
Generate Data URL from image file
import mimetypes
import base64
def make_data_url(filename):
prefix = 'data:{0};base64,'.format(mimetypes.guess_type(filename)[0])
with open(filename, 'rb') as f:
content = f.read()
data_url = prefix + base64.b64encode(content).decode()
return data_url
@wonderbeyond
wonderbeyond / shadowsocks-local.service
Last active November 27, 2017 15:36 — forked from ygmpkk/sslocal.service
ShadowSocks Client Systemd Service
[Unit]
Description=Shadowsocks Local Client
After=network.target
[Service]
ExecStart=/usr/bin/sslocal -c /home/wonder/.config/shadowsocks.json
Restart=on-abort
[Install]
WantedBy=multi-user.target
@wonderbeyond
wonderbeyond / graceful_shutdown_tornado_web_server.py
Last active February 22, 2023 05:34 — forked from mywaiting/graceful_shutdown_tornado_web_server.py
The example to how to shutdown tornado web server gracefully...
#!/usr/bin/env python
"""
How to use it:
1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID`,
The Tornado Web Server Will shutdown after process all the request.
2. When you run it behind Nginx, it can graceful reboot your production server.
"""
import time
def jsonp(f):
"""Wrap a json response in a callback, and set the mimetype (Content-Type) header accordingly
(will wrap in text/javascript if there is a callback). If the "callback" or "jsonp" paramters
are provided, will wrap the json output in callback({thejson})
Usage:
@jsonp
def my_json_view(request):
d = { 'key': 'value' }