Closed taoky closed 8 months ago
Thank you for referring https://github.com/aliyun/tea-util/pull/335 and the test results in production environments, though there's a minor issue with Python implementation (compared with Java/Golang implementation of same algorithm):
Due to python GIL, only one thread can be run.
A += 1
in Python would be compiled to multiple bytecodes (load, +=
and store):
>>> n = 0
>>> def f():
... global n
... n += 1
...
>>> import dis
>>> dis.dis(f)
1 0 RESUME 0
3 2 LOAD_GLOBAL 0 (n)
14 LOAD_CONST 1 (1)
16 BINARY_OP 13 (+=)
20 STORE_GLOBAL 0 (n)
22 LOAD_CONST 0 (None)
24 RETURN_VALUE
And in Python 3, GIL would be taken by another thread if it has timed out (https://github.com/python/cpython/blob/65c285062ce2769249610348636d3d73153e0144/Python/ceval_gil.c#L20-L24C58. GIL is also preemptive in Python 2.x), thus n
here is not an atomic variable. And unfortunately Python does not have built-in modules about atomics...
感谢 @taoky 对这个改动的关注。作为生成 Nonce 的实现,目前看下来还是 OK 的。这个讨论就暂时先关掉了。
如果还有其余问题,请 re-open。
API: N/A
In https://github.com/aliyun/aliyun-openapi-python-sdk/commit/7e03ac97ebbc421552ac47f2a8db85f2ecebb787,
get_uuid()
in aliyun-openapi-python-sdk/aliyun-python-sdk-core/aliyunsdkcore/utils/parameter_helper.py changes from:to:
However, to me this change sounds very weird as:
get_uuid()
returns is no longer a UUID (8-4-4-4-12 hexadecimal digits)._seqId += 1
is not thread-safe if multi thread are callingget_uuid()
(this increases the possibility of collisions).time.time()
is used as a part of generation here).It looks like a better idea to use Python's built-in
uuid
module instead of reinventing the wheel. It would be very appreciated if you (@h-hg as the author of this commit, I think) could explain this change. Thank you.