25 lines
796 B
Python
25 lines
796 B
Python
|
import json
|
||
|
from functools import wraps
|
||
|
from threading import Thread
|
||
|
|
||
|
def json_dumps(obj):
|
||
|
return json.dumps(obj, indent=4, default=_to_json, ensure_ascii=False, sort_keys=True).encode()
|
||
|
|
||
|
def _to_json(python_object):
|
||
|
if isinstance(python_object, datetime.datetime):
|
||
|
if python_object.year < 1900:
|
||
|
tt = python_object.timetuple()
|
||
|
return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
|
||
|
return python_object.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||
|
raise TypeError('%s %s is not JSON serializable' % (repr(python_object), type(python_object)))
|
||
|
|
||
|
def run_async(func):
|
||
|
@wraps(func)
|
||
|
def async_func(*args, **kwargs):
|
||
|
func_hl = Thread(target=func, args=args, kwargs=kwargs)
|
||
|
func_hl.start()
|
||
|
return func_hl
|
||
|
|
||
|
return async_func
|
||
|
|