0

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?

Babu
  • 2,548
  • 3
  • 30
  • 47

1 Answers1

1

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

mg.
  • 7,822
  • 1
  • 26
  • 30
  • I don't need to invoke `acquire()` and `release()` methods? – Babu Mar 07 '13 at 08:19
  • 1
    @Babu: you can use locks in a `with` context and they are invoked for you. Look at http://docs.python.org/2.7/library/threading.html#using-locks-conditions-and-semaphores-in-the-with-statement and http://stackoverflow.com/a/2738468/230454 – mg. Mar 07 '13 at 08:58