Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python's print synchronized?

Is python's print synchronized? :)

Between Threads.

like image 359
Skeen Avatar asked Dec 08 '10 23:12

Skeen


People also ask

How does Python print work?

The Python print() function takes in any number of parameters, and prints them out on one line of text. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char). When called with zero parameters, print() just prints the '\n' and nothing else.

Is print in Python thread safe?

The print() function is a built-in function for printing a string on stdout and is not thread-safe.

What is synchronized in Python?

The threading module provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method of the new lock object is used to force threads to run synchronously.


2 Answers

Python's print isn't even vaguely thread safe. (Bram Cohen's words, not mine.)

like image 111
ephemient Avatar answered Sep 28 '22 09:09

ephemient


if you call print from multiple threads, the output may be interleaved at the whim of the scheduler.

like image 40
Corey Goldberg Avatar answered Sep 28 '22 07:09

Corey Goldberg