Skip to content

Commit 46054af

Browse files
committed
chg: globals - json lib preference: orjson > ujson > json
1 parent adfa85e commit 46054af

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

glances/globals.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import base64
1717
import errno
1818
import functools
19+
import importlib
1920
import os
2021
import platform
2122
import queue
@@ -36,14 +37,27 @@
3637

3738
from defusedxml.xmlrpc import monkey_patch
3839

39-
# Optionally use orjson if available
40+
# Correct issue #1025 by monkey path the xmlrpc lib
41+
monkey_patch()
42+
43+
# Prefer faster libs for JSON (de)serialization
44+
# Preference Order: orjson > ujson > json (builtin)
4045
try:
4146
import orjson as json
47+
48+
json.dumps = functools.partial(json.dumps, option=json.OPT_NON_STR_KEYS)
4249
except ImportError:
43-
import json
50+
# Need to log info but importing logger will cause cyclic imports
51+
pass
4452

45-
# Correct issue #1025 by monkey path the xmlrpc lib
46-
monkey_patch()
53+
try:
54+
import ujson as json
55+
except ImportError:
56+
# Need to log info but importing logger will cause cyclic imports
57+
pass
58+
59+
if 'json' not in globals():
60+
import json
4761

4862
##############
4963
# GLOBALS VARS
@@ -303,7 +317,7 @@ def urlopen_auth(url, username, password):
303317
return urlopen(
304318
Request(
305319
url,
306-
headers={'Authorization': 'Basic ' + base64.b64encode((f'{username}:{password}').encode()).decode()},
320+
headers={'Authorization': 'Basic ' + base64.b64encode(f'{username}:{password}'.encode()).decode()},
307321
)
308322
)
309323

glances/plugins/ip/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
import threading
1212

13-
from orjson import loads
14-
15-
from glances.globals import queue, urlopen_auth
13+
from glances.globals import json_loads, queue, urlopen_auth
1614
from glances.logger import logger
1715
from glances.plugins.plugin.model import GlancesPluginModel
1816
from glances.timer import Timer, getTimeSinceLastUpdate
@@ -245,7 +243,7 @@ def _get_ip_public_info(self, queue_target, url, username, password):
245243
queue_target.put(None)
246244
else:
247245
try:
248-
queue_target.put(loads(response))
246+
queue_target.put(json_loads(response))
249247
except (ValueError, KeyError) as e:
250248
logger.debug(f"IP plugin - Cannot load public IP information from {url} ({e})")
251249
queue_target.put(None)

0 commit comments

Comments
 (0)