0

I'm using a Multiprocessing.Queue to communicate between my processing processes and my daemon process. The daemon takes output from the queue and writes to file in an infinite loop. The file object is opened in the daemon process printToFile itself.

resultqueue = Queue()
p = Process(target = printToFile , args=(resultqueue))
p.daemon = True
p.start()
for si, ei in ranges:
 pr = Process(target = processing , args=(si, ei, resultqueue))
 pr.start()
 processes.append(pr)
for pr in processes:
 pr.join()

My problem is that printToFile doesn't write anything to file, even though it prints to screen, the output it gets from the queue. When I remove the line, setting it to a daemon process, and manually kill the program using Ctrl+C, everything works fine. Can someone please help me understand what is going on. I don't know where to start debugging.

I am not using fileObject.close() as the daemon dies when the program finished execution. But I don't think that is the problem, because the program does write to file when I use Ctrl+C without making the process daemon. Another (maybe unrelated) problem, is that when the file object is not instantiated within printToFile, but is a global, even using Ctrl+C doesn't print the output to file. But I can live with that. But I would still like to understand what's going on.

asked Dec 9, 2013 at 23:51
5
  • 2
    Try to open file with 0 as third parameter of open function Commented Dec 9, 2013 at 23:54
  • Can you give us a complete, runnable example? See SSCCE for guidance. As it is, it looks like you're asking us to debug code inside a function that you haven't shown us the code for. Commented Dec 9, 2013 at 23:59
  • Thanks @Deck , that was it, can't believe I didn't know about that. Commented Dec 10, 2013 at 0:07
  • have you tried to call file.flush() when you need to make the results available? Commented Dec 10, 2013 at 8:32
  • I guess that would work too, since the problem was buffered data. Commented Dec 11, 2013 at 2:24

1 Answer 1

4

Since my guess was successful in comments I would point out what I said. open() function has third argument - buffering:

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.

Passing 0 as third argument you open a file in unbuffered mode so the changes appears there immediately.

answered Dec 10, 2013 at 0:14
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.