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
#2
You need make sure the main thread stays with the child thread to see the 3 sec frequency. And to do this join the child thread with the main and therefore the main wont exit. Try the following.

td = None
def startThread( ):
    global td
    print( "Starting thread.")
    td = TestThread( )
    td.setDaemon( True )
    td.start( )
    td.join()
Reply
#3
Thanks for the response but adding the join prohibits the user from entering commands which needs to be allowed. I just need messages printed by the thread to remain.
Reply
#4
def startThread():
    global td
    print("Starting thread.")
    td = TestThread()
    td.setDaemon(True)
    td.start()
    input("Press any key to exit")
    td.close()
Reply
#5
Thanks for the response. I don't think this will work either. I don't want to stop the user from entering more python commands. I just need to have both the thread running in the background while the main python interpreter waiting or executing additional commands. I just need the message from the thread to remain visible even if a key is pressed.
Reply
#6
(Jan-15-2018, 03:22 PM)bweiss1258 Wrote: The issue is that when any key is pressed the all the "hello world" text disappears.
I don't have this issue in a kubuntu terminal. The bug may be specific to your environment.
Reply
#7
I'm running this on Windows because that's what our customer will use but I did try it on a Linux box I have in my office and your right I didn't have an issue. I wonder what's different about python running on Windows that's causing the issue.
Reply
#8
How are you running it on windows machine. Command line or do you use IDE?
Reply
#9
(Jan-15-2018, 09:22 PM)bweiss1258 Wrote: I wonder what's different about python running on Windows that's causing the issue.
You should never run thread in IDE/IDLE or editor,always from command line(cmd).
It will/can interfer with the main loop that IDE/IDLE or editor is already running.

If you tell more what you try to solve,we can maybe come up with better a solution.
Example running stuff in schedule Python job scheduling for humans.
Reply
#10
I'm running from the command line. For the project I'm on the thread is polling a target to determine if an event has occurred and if so outputs a message to the console indicating that an event was detected. The user can then enter commands at the python prompt for more info. I've tried the example code I post on Linux and on my Mac and don't not have the issues I'm seeing on my Windows PC.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question: print issue python202209 3 922 Sep-18-2022, 11:51 AM
Last Post: jefsummers
  Why doesn't this print statement work? stylingpat 10 5,591 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 2,747 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  issue with if else statement spalisetty06 6 2,346 Jul-02-2020, 07:50 AM
Last Post: spalisetty06
  Taking brackets out of list in print statement pythonprogrammer 3 2,338 Apr-13-2020, 12:25 PM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,516 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  printing a list contents without brackets in a print statement paracelx 1 2,081 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,390 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Embedding return in a print statement Tapster 3 2,233 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Quotes and variables in print statement Mark17 4 2,994 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