Giter Club home page Giter Club logo

Comments (3)

h-hg avatar h-hg commented on August 22, 2024
  1. This function is used to generate a unique nonce value for the openapi call, and we don't limit the format to satisfy the uuid. For the historical reasons, the function name may be confusing to readers.
  2. This is a Python2 repository. Due to python GIL, only one thread can be run. If this GIL assumption does not hold, the randNum field can be used to reduce the collision rate.
  3. We have tested the this design in tea-util in the real environment, we found that the collisions has been significantly reduced.

from aliyun-openapi-python-sdk.

taoky avatar taoky commented on August 22, 2024

Thank you for referring aliyun/tea-util#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...

from aliyun-openapi-python-sdk.

JacksonTian avatar JacksonTian commented on August 22, 2024

感谢 @taoky 对这个改动的关注。作为生成 Nonce 的实现,目前看下来还是 OK 的。这个讨论就暂时先关掉了。

如果还有其余问题,请 re-open。

from aliyun-openapi-python-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.