0

I am trying to write something in a file by a daemon thread. The problem is that the file is being created but it is empty. Is it possible to write in a file by a daemon thread?

The reason of using daemon thread is because my main program will terminate first as compared to my thread. So in order to keep that thread running even after the execution of the program daemon is used.

Below is the code:

import threading

def hlp():
    with open("/path/to/y.txt",mode="w") as f:
        f.write("Hello")

def test():
    thr=threading.Thread(target=hlp)
    thr.daemon=True
    thr.start()

test()
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Amay
  • 104
  • 6

2 Answers2

2

Coming from here, It looks like you need to join the thread after you start it when you're playing with a daemon thread:

In other words:

with join - interpreter will wait until your process get completed or terminated, in this case file being written

import threading
def hlp():
    with open("C:\\Users\\munir.khan\\PycharmProjects\\opencv-basics2019-03-22_14-49-26\\y.txt",mode="a+") as f:
        f.write("Hello")

def test():
    thr=threading.Thread(target=hlp)
    thr.daemon=True
    thr.start()
    thr.join()

test()

OUTPUT:

Hello

EDIT:

If you do not want to use join, you may set thr.daemon=False, but I don't like it since, it says here that Setting thread.daemon = True allows the main program to exit.

import threading
def hlp():
    with open("C:\\Users\\munir.khan\\PycharmProjects\\opencv-basics2019-03-22_14-49-26\\y.txt",mode="a+") as f:
        f.write("Hello")

def test():
    thr=threading.Thread(target=hlp)
    thr.daemon=False
    thr.start()

test()
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • join makes the main program wait till the execution of the thread is done. I don't want that. I want the program to run as it and terminate if it wants to but the thread should still keep running till it finishes its work and not make main thread wait. – Amay Mar 26 '19 at 07:00
  • @Amay but if you must insist, use `thr.daemon=False` but I won't recommend it. – DirtyBit Mar 26 '19 at 07:10
  • Daemon's thread purpose is to run the thread even after the execution of the main program. So I think daemon thread maybe the right way. – Amay Mar 26 '19 at 07:11
  • Yeah now I am able to find the text in the file. The thread is able to append but not write? But you have removed the daemon thread, which I wanted to use for my case – Amay Mar 26 '19 at 07:28
  • In the example using `thr.daemon=False`, (which you can omit, `False` is the ["default" as inherited from the main thread](https://docs.python.org/3/library/threading.html#threading.Thread.daemon), after all), `join()` _is_ called implicitly when [`threading._shutdown`](https://github.com/python/cpython/blob/3.7/Lib/threading.py#L1279-L1282) is executed. That function is called during interpreter shutdown in [`wait_for_thread_shutdown`](https://github.com/python/cpython/blob/master/Python/pylifecycle.c#L2164). Non-daemonic threads will be joined, either explicitly or implicitly. – shmee Mar 26 '19 at 10:14
1

Using a daemon thread might not be what you want since a daemon thread won't wait for the thread to complete before the program exits.

If you still want to use a daemon you should use .join() so that you wait for that thread to finish.

Example:

import threading

def hlp():
    with open("/path/to/y.txt",mode="w") as f:
        f.write("Hello")

def test():
    thr=threading.Thread(target=hlp)
    thr.daemon=True
    thr.start()
    thr.join()

test()

Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34