Re: Using threads in python is safe ?

Discussion in 'Python' started by Gabriel Genellina, Mar 16, 2008.

  1. En Sat, 15 Mar 2008 11:57:44 -0200, Deepak Rokade <>
    escribi�:

    > I want to use therads in my application. Going through the docs , I read
    > about GIL.
    > Now I am confused whether using threads in python is safe or not.
    >
    > One thing I know that if I am accessing global variables in two or more
    > threads I need to synchronize them
    > using locking or such mechanism so that only one thread access them at a
    > time.


    Yes, altough some operations are known to be atomic so you don't need a
    lock in that cases. I think there is a list in the wiki somewhere
    http://wiki.python.org/moin or perhaps at the effbot's site
    http://www.effbot.org

    > 1. In order to support multi-threaded Python programs, there's a global
    > lock that must be held
    > by the current thread before it can safely access Python objects.
    > Does this lock need to be held by python application script expliciltly
    > before accessing any python object or
    > interpreter takes acre of it ?


    No, the interpreter takes care of it. The GIL is a concern for those
    writing extensions using the Python API.

    > 2. Does multithreaded python script need to held lock before calling any
    > blocking I/O call?
    > Or one should not worry about GIL while using python threads if job to be
    > processed by thread does not call
    > any global variables and thread unsafe Python/C extension ?


    Python code should not worry about the GIL. The problem would be, a
    callback written in Python for a not-thread-aware extension that had
    released the GIL.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 16, 2008
    #1
    1. Advertising

  2. Gabriel Genellina

    Benjamin Guest

    On Mar 16, 3:40 pm, "Gabriel Genellina" <>
    wrote:
    > En Sat, 15 Mar 2008 11:57:44 -0200, Deepak Rokade <>
    > escribi�:
    >
    > > I want to use therads in my application. Going through the docs , I read
    > > about GIL.
    > > Now I am confused whether using threads in python is safe or not.

    >
    > > One thing I know that if I am accessing global variables in two or more
    > > threads I need to synchronize them
    > > using locking or such mechanism so that only one thread access them at a
    > > time.

    >
    > Yes, altough some operations are known to be atomic so you don't need a
    > lock in that cases. I think there is a list in the wiki somewhere http://wiki.python.org/moinor perhaps at the effbot's site http://www.effbot.org

    Even for atomic operations, you should lock, though. That is not
    consistent over different Python implementations and is not always
    going to be in same in CPython.
    >
    > > 1. In order to support multi-threaded Python programs, there's a global
    > > lock that must be held
    > > by the current thread before it can safely access Python objects.
    > > Does this lock need to be held by python application script expliciltly
    > > before accessing any python object or
    > > interpreter takes acre of it ?

    >
    > No, the interpreter takes care of it. The GIL is a concern for those
    > writing extensions using the Python API.
    >
    > > 2. Does multithreaded python script need to held lock before calling any
    > > blocking I/O call?
    > > Or one should not worry about GIL while using python threads if job to be
    > > processed by thread does not call
    > > any global variables and thread unsafe Python/C extension ?

    >
    > Python code should not worry about the GIL. The problem would be, a
    > callback written in Python for a not-thread-aware extension that had
    > released the GIL.
    >
    > --
    > Gabriel Genellina
    Benjamin, Mar 17, 2008
    #2
    1. Advertising

  3. On Wed, Mar 19, 2008 at 7:43 AM, Deepak Rokade <> wrote:
    > If jobs to be processed by threds is I/O bound would multithreading help
    > python to improve speed of application ?


    Probably, yes.

    --
    Cheers,
    Simon B.

    http://www.brunningonline.net/simon/blog/
    Simon Brunning, Mar 19, 2008
    #3
  4. En Wed, 19 Mar 2008 04:43:34 -0300, Deepak Rokade <>
    escribió:

    > Thanks all for removing confusion about GIL,
    > one more question;
    > If jobs to be processed by threds is I/O bound would multithreading help
    > python to improve speed of application ?
    > Since I read that " multithreading is not a good strategy to improve
    > speed
    > of python application."


    Try and see what happens in your specific case. People keeps saying that
    but I've never seen conclusive evidence on this topic. Using several
    threads for I/O bound tasks seems reasonable to me, and is the easiest
    approach. For CPU bound tasks, multiple threads won't help much, be it
    Python or another language.
    I mean, a reasonable number of threads with a reasonable task load. A
    server handling thousands of simultaneous connections is another beast.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 19, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Rossetti
    Replies:
    0
    Views:
    1,314
    Gabriel Rossetti
    Aug 29, 2008
  2. Replies:
    1
    Views:
    334
    Brian Candler
    Aug 12, 2003
  3. Aredridel

    Not just $SAFE, but damn $SAFE

    Aredridel, Sep 2, 2004, in forum: Ruby
    Replies:
    19
    Views:
    240
  4. Farrel Lifson

    $SAFE =4 safe enough?

    Farrel Lifson, Aug 29, 2006, in forum: Ruby
    Replies:
    7
    Views:
    101
    Eric Hodel
    Aug 31, 2006
  5. John Nagle
    Replies:
    5
    Views:
    466
    John Nagle
    Mar 12, 2012
Loading...

Share This Page