chg: globals - json lib preference: orjson > ujson > json

This commit is contained in:
RazCrimson 2024-09-08 12:49:23 +05:30
parent adfa85e8ab
commit 46054afd22
2 changed files with 23 additions and 11 deletions

View File

@ -16,6 +16,7 @@
import base64
import errno
import functools
import importlib
import os
import platform
import queue
@ -36,15 +37,28 @@ from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
from defusedxml.xmlrpc import monkey_patch
# Optionally use orjson if available
try:
import orjson as json
except ImportError:
import json
# Correct issue #1025 by monkey path the xmlrpc lib
monkey_patch()
# Prefer faster libs for JSON (de)serialization
# Preference Order: orjson > ujson > json (builtin)
try:
import orjson as json
json.dumps = functools.partial(json.dumps, option=json.OPT_NON_STR_KEYS)
except ImportError:
# Need to log info but importing logger will cause cyclic imports
pass
try:
import ujson as json
except ImportError:
# Need to log info but importing logger will cause cyclic imports
pass
if 'json' not in globals():
import json
##############
# GLOBALS VARS
##############
@ -303,7 +317,7 @@ def urlopen_auth(url, username, password):
return urlopen(
Request(
url,
headers={'Authorization': 'Basic ' + base64.b64encode((f'{username}:{password}').encode()).decode()},
headers={'Authorization': 'Basic ' + base64.b64encode(f'{username}:{password}'.encode()).decode()},
)
)

View File

@ -10,9 +10,7 @@
import threading
from orjson import loads
from glances.globals import queue, urlopen_auth
from glances.globals import json_loads, queue, urlopen_auth
from glances.logger import logger
from glances.plugins.plugin.model import GlancesPluginModel
from glances.timer import Timer, getTimeSinceLastUpdate
@ -245,7 +243,7 @@ class PublicIpInfo:
queue_target.put(None)
else:
try:
queue_target.put(loads(response))
queue_target.put(json_loads(response))
except (ValueError, KeyError) as e:
logger.debug(f"IP plugin - Cannot load public IP information from {url} ({e})")
queue_target.put(None)