atomic section in code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmad Humayun

    atomic section in code

    Hi there,

    I need to create an atomic section in Python code i.e. there is no
    context switch to any other thread during the running of that piece of
    code. Would would do the trick?


    thanks,

  • Fredrik Lundh

    #2
    Re: atomic section in code

    Ahmad Humayun wrote:
    I need to create an atomic section in Python code i.e. there is no
    context switch to any other thread during the running of that piece of
    code. Would would do the trick?
    use a lock, and make sure that anyone that needs access to the shared
    state you're manipulating in that section uses the same lock.

    lock = threading.Lock( ) # or RLock() etc [1]

    with lock:
    section

    this only works if everyone honors the lock, of course; there's no way
    in Python to lock out non-cooperating external threads.

    </F>

    1) see http://effbot.org/zone/thread-synchronization.htm

    Comment

    • Ahmad Humayun

      #3
      Re: atomic section in code

      On Sep 5, 1:59 pm, Fredrik Lundh <fred...@python ware.comwrote:
      Ahmad Humayun wrote:
      I need to create an atomic section in Python code i.e. there is no
      context switch to any other thread during the running of that piece of
      code. Would would do the trick?
      >
      use a lock, and make sure that anyone that needs access to the shared
      state you're manipulating in that section uses the same lock.
      >
           lock = threading.Lock( ) # or RLock() etc [1]
      >
           with lock:
               section
      >
      this only works if everyone honors the lock, of course; there's no way
      in Python to lock out non-cooperating external threads.
      >
      </F>
      >
      1) seehttp://effbot.org/zone/thread-synchronization .htm
      Thats true, but this will ensure mutual exclusion; not atomicity

      thanks again,

      Comment

      • Diez B. Roggisch

        #4
        Re: atomic section in code

        Ahmad Humayun schrieb:
        On Sep 5, 1:59 pm, Fredrik Lundh <fred...@python ware.comwrote:
        >Ahmad Humayun wrote:
        >>I need to create an atomic section in Python code i.e. there is no
        >>context switch to any other thread during the running of that piece of
        >>code. Would would do the trick?
        >use a lock, and make sure that anyone that needs access to the shared
        >state you're manipulating in that section uses the same lock.
        >>
        > lock = threading.Lock( ) # or RLock() etc [1]
        >>
        > with lock:
        > section
        >>
        >this only works if everyone honors the lock, of course; there's no way
        >in Python to lock out non-cooperating external threads.
        >>
        ></F>
        >>
        >1) seehttp://effbot.org/zone/thread-synchronization .htm
        >
        Thats true, but this will ensure mutual exclusion; not atomicity
        AFAIC that kind of mutual exclusion is what atomicity is about. What
        else do you expect to happen?

        Diez

        Comment

        • Michele Simionato

          #5
          Re: atomic section in code

          On Sep 5, 10:59 am, Fredrik Lundh <fred...@python ware.comwrote:
          >
          1) seehttp://effbot.org/zone/thread-synchronization .htm
          The page you link here is WAYS better than the standard documentation
          of the threading module.
          Generally speaking, the effbot zone contains a lot of improvements
          over the standard docs
          which are lacking in various areas.
          I have always wondered why they are kept separated. Wouldn't be nice
          to have the standard
          docs integrated with the effbot docs? Are there plans in this sense
          and if not, why not?

          Michele Simionato

          Comment

          • Fredrik Lundh

            #6
            Documentation (was Re: atomic section in code)

            Michele Simionato wrote:
            The page you link here is WAYS better than the standard documentation
            of the threading module.
            Generally speaking, the effbot zone contains a lot of improvements
            over the standard docs which are lacking in various areas.
            I have always wondered why they are kept separated. Wouldn't be nice
            to have the standard
            docs integrated with the effbot docs? Are there plans in this sense
            and if not, why not?
            There are tons of great supplementary material out there, on blogs,
            personal documentation collections (such as effbot.org), cookbook sites,
            etc. I don't think all that material absolutely must be posted to
            python.org, but Python users would definitely benefit from improved
            cross-linking.

            And this is, of course, something I've lobbied for many times, e.g.





            but I haven't yet figured out how to get something to happen [1]. All
            ideas are welcome.

            </F>

            1) Well, I guess the new Sphinx tool might have gotten some inspiration
            by my work in this domain:



            and Andrew Kuchling did some preliminary work for the old Latex work flow:



            Comment

            • Fredrik Lundh

              #7
              Re: atomic section in code

              Diez B. Roggisch wrote:
              AFAIC that kind of mutual exclusion is what atomicity is about. What
              else do you expect to happen?
              sounds like he wants/needs non-cooperative, mandatory locking.

              </F>

              Comment

              Working...