Are range iterators thread safe?

Discussion in 'Python' started by Steven D'Aprano, Oct 20, 2011.

  1. Using Python 3, are range_iterator objects thread-safe?

    I have tried this, and it seems to be safe:

    >>> from threading import Thread
    >>> x = iter(range(4))
    >>> def doit(x):

    .... print("result =", next(x))
    ....
    >>> threads = [Thread(target=doit, args=(x,)) for i in range(4)]
    >>> for t in threads:

    .... t.start()
    ....
    result = 0
    result = 1
    result = 2
    result = 3
    >>>




    --
    Steven
     
    Steven D'Aprano, Oct 20, 2011
    #1
    1. Advertising

  2. Steven D'Aprano, 20.10.2011 10:04:
    > Using Python 3, are range_iterator objects thread-safe?
    >
    > I have tried this, and it seems to be safe:
    >
    > >>> from threading import Thread
    > >>> x = iter(range(4))
    > >>> def doit(x):

    > ... print("result =", next(x))
    > ...
    > >>> threads = [Thread(target=doit, args=(x,)) for i in range(4)]
    > >>> for t in threads:

    > ... t.start()
    > ...
    > result = 0
    > result = 1
    > result = 2
    > result = 3


    The GIL ensures it's thread safe.

    Stefan
     
    Stefan Behnel, Oct 20, 2011
    #2
    1. Advertising

  3. Ben Finney, 20.10.2011 13:23:
    > Stefan Behnel writes:
    >
    >> Steven D'Aprano, 20.10.2011 10:04:
    >>> Using Python 3, are range_iterator objects thread-safe?

    >> The GIL ensures it's thread safe.

    >
    > The GIL applies only to CPython.


    and PyPy.


    > What is the answer for other Python
    > implementations which don't have a GIL?


    That would basically be Jython and IronPython.

    Note that none of the three alternative implementations currently supports
    Python language version 3. So, the current answer for all existing Python 3
    implementations is: the GIL ensures that it's thread safe.

    Stefan
     
    Stefan Behnel, Oct 20, 2011
    #3
  4. Steven D'Aprano

    Ian Kelly Guest

    On Thu, Oct 20, 2011 at 3:22 AM, Stefan Behnel <> wrote:
    > Steven D'Aprano, 20.10.2011 10:04:
    >>
    >> Using Python 3, are range_iterator objects thread-safe?
    >>
    >> I have tried this, and it seems to be safe:
    >>
    >> >>> from threading import Thread
    >> >>> x = iter(range(4))
    >> >>> def doit(x):

    >> ...     print("result =", next(x))
    >> ...
    >> >>> threads = [Thread(target=doit, args=(x,)) for i in range(4)]
    >> >>> for t in threads:

    >> ...     t.start()
    >> ...
    >> result = 0
    >> result = 1
    >> result = 2
    >> result = 3

    >
    > The GIL ensures it's thread safe.


    Because range_iterator objects are implemented in C and so calling the
    __next__ method is only a single bytecode instruction, correct? If
    they were implemented in Python the GIL would make no such assurance.
     
    Ian Kelly, Oct 20, 2011
    #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. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    510
    Kai-Uwe Bux
    May 8, 2005
  2. Henrik Goldman
    Replies:
    3
    Views:
    457
    Henrik Goldman
    Apr 23, 2006
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,376
    Gabriel Rossetti
    Aug 29, 2008
  4. , India
    Replies:
    10
    Views:
    1,099
    James Kanze
    Aug 8, 2009
  5. John Nagle
    Replies:
    5
    Views:
    496
    John Nagle
    Mar 12, 2012
Loading...

Share This Page