Is the standard output thread-safe?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodríguez

    Is the standard output thread-safe?


    Hi,

    Is the standard output thread-safe? Can I use print from several threads
    without having to use a mutex?

    Thanks


  • danmcleran@yahoo.com

    #2
    Re: Is the standard output thread-safe?

    >Is the standard output thread-safe?
    No.

    You'll need to synchronize access.

    Comment

    • Fredrik Lundh

      #3
      Re: Is the standard output thread-safe?

      Fernando Rodríguez wrote:
      [color=blue]
      > Is the standard output thread-safe? Can I use print from several threads
      > without having to use a mutex?[/color]

      if you use sys.stdout.writ e on the standard sys.stdout stream, each write
      operation is "atomic" (thanks to the GIL).

      if you use other approaches (including print statements, or sys.stdout re-
      direction via python code), data from different streams may appear out of
      order. python won't crash, though.

      </F>



      Comment

      • Fernando Rodríguez

        #4
        Re: Is the standard output thread-safe?

        Hello Fredrik,
        [color=blue]
        > Fernando Rodríguez wrote:
        >[color=green]
        >> Is the standard output thread-safe? Can I use print from several
        >> threads without having to use a mutex?
        >>[/color]
        > if you use sys.stdout.writ e on the standard sys.stdout stream, each
        > write operation is "atomic" (thanks to the GIL).[/color]

        GIL?


        Comment

        • Felipe Almeida Lessa

          #5
          Re: Is the standard output thread-safe?

          Em Qui, 2006-03-09 às 16:37 +0000, Fernando Rodríguez escreveu:[color=blue]
          > Hello Fredrik,
          >[color=green]
          > > Fernando Rodríguez wrote:
          > >[color=darkred]
          > >> Is the standard output thread-safe? Can I use print from several
          > >> threads without having to use a mutex?
          > >>[/color]
          > > if you use sys.stdout.writ e on the standard sys.stdout stream, each
          > > write operation is "atomic" (thanks to the GIL).[/color]
          >
          > GIL?
          >[/color]

          Global Interpreter Lock

          --
          "Quem excele em empregar a força militar subjulga os exércitos dos
          outros povos sem travar batalha, toma cidades fortificadas dos outros
          povos sem as atacar e destrói os estados dos outros povos sem lutas
          prolongadas. Deve lutar sob o Céu com o propósito primordial da
          'preservação' . Desse modo suas armas não se embotarão, e os ganhos
          poderão ser preservados. Essa é a estratégia para planejar ofensivas."

          -- Sun Tzu, em "A arte da guerra"

          Comment

          • Skull

            #6
            Re: Is the standard output thread-safe?

            Fernando Rodríguez <frr@easyjob.ne t> writes:
            [color=blue]
            > Hi,
            >
            > Is the standard output thread-safe? Can I use print from several
            > threads without having to use a mutex?
            >
            > Thanks
            >
            >[/color]

            Check this: http://me.in-berlin.de/doc/python/fa...re-thread-safe

            from my experience, it seems 'print' is not thread-safe. especially
            when it is called with formated string, such as:

            print "id:%d", my_id

            I think it is not an "atomic" operation.

            --

            Comment

            Working...