Python Forum
Issue using print statement in a thread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue using print statement in a thread
#1
I'm having an issue with text output from a thread staying visible in the interpreter. The code below can be used to demonstrate the problem following these steps.

1. Place the code into a file.
2. Start Python 2.7 and import the file.
3. Execute the startThread() function to start the thread.

At this point you should see "hello world" printed every 3 seconds. The issue is that when any key is pressed the all the "hello world" text disappears. I need to find a way to maintain the text printed by the thread in the interpreter. Any ideas or help is appreciated.

from __future__ import print_function
import threading
import time

class TestThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self, name='test')
        self._done = False

    def run(self):
        while not self._done:
            print("hello world")
            time.sleep( 3 )

    def close(self):
        self._done = True

td = None
def startThread( ):
    global td
    print( "Starting thread.")
    td = TestThread( )
    td.setDaemon( True )
    td.start( )
Reply


Messages In This Thread
Issue using print statement in a thread - by bweiss1258 - Jan-15-2018, 03:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question: print issue python202209 3 1,076 Sep-18-2022, 11:51 AM
Last Post: jefsummers
  Why doesn't this print statement work? stylingpat 10 6,135 Mar-23-2021, 07:54 PM
Last Post: buran
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 3,009 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  issue with if else statement spalisetty06 6 2,636 Jul-02-2020, 07:50 AM
Last Post: spalisetty06
  Taking brackets out of list in print statement pythonprogrammer 3 2,525 Apr-13-2020, 12:25 PM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,679 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  printing a list contents without brackets in a print statement paracelx 1 2,222 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,958 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Embedding return in a print statement Tapster 3 2,443 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Quotes and variables in print statement Mark17 4 3,185 Sep-13-2019, 04:07 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020