file write collision consideration

R

RGK

I have a thread that is off reading things some of which will get
written into a file while another UI thread manages input from a user.

The reader-thread and the UI-thread will both want to write stuff to the
same output file. What first comes to mind is that there may be write
collisions, ie both trying to write at the same time.

Should I do something like:


if busyFlag:
while busyFlag:
sleep(10)
else:
busyFlag = True
{write stuff to file}
busyFlag = False


in both threads? Is there some other approach that would be more
appropriate?

Thanks in advance for your advice
- Ross.
 
D

D'Arcy J.M. Cain

I have a thread that is off reading things some of which will get
written into a file while another UI thread manages input from a user.

The reader-thread and the UI-thread will both want to write stuff to the
same output file. What first comes to mind is that there may be write
collisions, ie both trying to write at the same time.

Why not create a third thread that handles the write? The other
threads can simply add objects to a queue. You will still need
collision handling in the queue adder but it only needs to block for a
small time as opposed to the much longer disk write time.
 
M

MRAB

RGK said:
I have a thread that is off reading things some of which will get
written into a file while another UI thread manages input from a user.

The reader-thread and the UI-thread will both want to write stuff to the
same output file. What first comes to mind is that there may be write
collisions, ie both trying to write at the same time.

Should I do something like:


if busyFlag:
while busyFlag:
sleep(10)
else:
busyFlag = True
{write stuff to file}
busyFlag = False


in both threads? Is there some other approach that would be more
appropriate?

Thanks in advance for your advice
If you're using the threading module, I suggest you look at the
threading.RLock class:

my_lock = threading.RLock()
....
with my_lock:
{write stuff to file}

or, if you're using an older version of Python:

my_lock = threading.RLock()
....
my_lock.acquire()
{write stuff to file}
my_lock.release()
 
R

RGK

Thanks for the suggestions - sounds like a couple good options, I
apprecieate it.

Ross.
 
G

Gary

It would help to know which version of Python when giving examples...

I recollect that so-called mutex operation wasn't actually thread safe when
using Python 2.5, but perhaps that was wrong, or subsequent versions have
fixed that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top