1

I have a python script which consists of several threads running and they have print outs to tell any connection failure, e.g. failed to open ports, failed to connect etc.

I've tried the basic:

import sys
sys.stdout = open('file', 'w')

but nothing was saved into the file. I'm trying to get the output of the system print outs and save it into tinydb. I am trying to log all of the outputs into tinydb. How can I do this?

Is it possible to just retrieve all of the print outputs continuously and save it into a json file using tinydb?

4
  • That's a python2 or python3 question? How do you actually print. How are your thread actually started. Commented Oct 19, 2017 at 6:47
  • Not exactly a duplicate but related stackoverflow.com/questions/9316023/python-2-7-print-to-file Commented Oct 19, 2017 at 6:48
  • It is a python3 question. Printing using the basic print("...."). Thread starts using threading.Thread() Commented Oct 19, 2017 at 6:49
  • @Loïc Faure-Lacroix link doesn't seem to answer my question.. Commented Oct 19, 2017 at 6:56

1 Answer 1

0

In python3, you have to use print with a file argument if you want to override stdout as the documentation says and as documented in this question:

Python 2.7: Print to File

This gives you the basic building block for achieve what you're looking for. You're not really explaining anything on how you expect to use it with tinydb...

That said, your print call should look like this:

fake_stdout = open('file', 'w')
print(data, file=fake_stdout)

That's it.

If for some reasons you have a lot of print statement to change, you could do something like this:

Have a module with a thread local variable that get initialized. Here look at this question: Thread local storage in Python

Then when you have your module set up you can add this statement in all your files that needs a different print stdout

from special_print import local_print as print

All you have to do is to define a threadlocal variable in your special_print module and then define it when a new thread starts with something like this:

def print_factory(fout):
    def local_print(arg):
        print(arg, file=fout)
    return local_print

In each new thread you run something like this:

import special_print
special_print.local_print = print_factory(fake_stdout)

Then when you import this print method in each of your threads, they'll correctly output the data to the fake_stdout you defined in each thread.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.