top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is print thread safe in Python (2.6/2.7)?

0 votes
709 views

Is print thread safe? That is, if I have two threads that each call print, say:

print "spam spam spam" # thread 1
print "eggs eggs eggs" # thread 2

I don't care which line prints first, but I do care if the two lines are mixed in together, something like this:

spam spaeggs eggs m seggspams

Does print perform its own locking to prevent this?

posted Aug 11, 2014 by Jai Prakash

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

On Python 3, print is thread safe.

But Python 2 has broken scenario:

print "spam", "spam", "spam" # thread 1
print "eggs", "eggs", "eggs" # thread 2

In this case, 2 lines are mixed.

In your case, "spam spam spam" and "eggs eggs eggs" are not mixed, but newline is mixed like:

spam spam spameggs eggs eggs
eggs eggs eggsspam spam spam
answer Aug 11, 2014 by Naveena Garg
Similar Questions
+3 votes

I am using 2.7. I need to print the time in seconds from the epoch with millisecond precision. I have tried many things but have failed.

 from time import time, strftime
 from datetime import datetime, time

 # write date, time, then seconds from epoch
 self.logfile.write('%st'%(strftime("%Y-%m-%d",)))
 self.logfile.write('%st'%(now.strftime("%H:%M:%S",)))
 self.logfile.write('%st'%(now.time()))

What am i doing wrong? What should I be doing here?

+3 votes

I have the following script however when the clipboard contents are greek letters it fails to print them right.
I have used all posible encoding for greek letters including utf8 but to no avail so i just stay with latin1.

Can you suggest a solution?

Here is the script:

import win32clipboard
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
data = data.decode('latin1')
send = 'ccc|=:='+data
print data
print send 
+1 vote

Something that just sends a screen directly to the printer without having to involve Notepad or the like? I am unable to find a search function on the updated Yahoo group.

+3 votes

I'm new to Python. I downloaded the 64-bit build of Python 2.7.10 from www.python.org/downloads/release/python-2710. I run Windows 7 Pro on a Dell PC.

I find that the installation package creates a folder called "Python 2.7" in my Start menu, with both a command prompt and IDLE GUI options.

I hold my scripts in another directory that is parallel to but not under the one where Python 2.7 resides, so I set the Environment Variable PYTHONPATH to include the directory where my scripts reside. Unfortunately, I am unable to reach them in either the IDLE GUI or the "Python command line" in that folder in my start menu. The only way I have managed to run Python scripts is to open an ordinary command prompt from Accessories, navigate to the directory with the scripts, and run python from the command prompt in that directory.

Can anyone let me know what I'm not doing that I should be doing to run Python and my scripts from the parallel directory under the GUI or the command line from the start menu folder ?

+2 votes

I look after a Delphi program that uses Python 2.5 (via Python for Delphi). A customer who uses a modeling program that requires Python 2.7 experiences a Python conflict when trying to run the Delphi program. I have installed both Python 2.5 and 2.7 on a test-bed computer and can run the Delphi program. I have searched the FAQ, and have found some mention of being able to set a default Python version when installing, which I presume has occurred when the customer installed Python 2.7, so that the Delphi program is being directed to Python 2.7.

However, I do not see this option when I install Python 2.7, and I do not see how to remove this option so I can advise the customer what to do. The programs are running under Windows 7 - 32-bit. Any assistance gratefully received.

...