Babu
Babu

Reputation: 2598

Python threading in cron - lock stderr file

I'm gonna schedule a cron with these options in my Ubuntu machine,

* */1 * * * python /path/to/script/my_script.py --pythonpath=/path/to/script/ 1>/path/to/script/success.log 2>/path/to/script/error.log

which will branch out n number of threads inside. And for logging exceptions(if raised) in each threads I'm using python's print >>stderr. Question is, if two threads try to write exception on same time will it cause concurrency problem? And if yes, how can I lock and release the stderr file from thread?

Upvotes: 0

Views: 650

Answers (1)

mg.
mg.

Reputation: 8012

Yes, you can have half stacktrace from one thread and then the stacktrace from another one thread.

And if yes, how can I lock and release the stderr file from thread

like every other resource shared between threads, with a lock:

import threading

stderr_l = threading.Lock()

def print_t(msg):
    with stderr_l:
        print msg

and then use only print_t from your threads

Upvotes: 1

Related Questions