Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file write collision consideration

Reply
Thread Tools

file write collision consideration

 
 
RGK
Guest
Posts: n/a
 
      01-20-2009
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.
 
Reply With Quote
 
 
 
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      01-20-2009
On Tue, 20 Jan 2009 10:57:52 -0500
RGK <> wrote:
> 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.

--
D'Arcy J.M. Cain <> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-20-2009
RGK wrote:
> 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()
 
Reply With Quote
 
RGK
Guest
Posts: n/a
 
      01-20-2009
Thanks for the suggestions - sounds like a couple good options, I
apprecieate it.

Ross.

MRAB wrote:
> RGK wrote:
>> 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()

 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      01-22-2009
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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mpls bgp consideration nini Cisco 0 11-26-2008 08:16 AM
about design consideration of strcpy() steve yee C Programming 13 05-26-2006 03:00 PM
Letter-of-consideration =?Utf-8?B?Z3RyaWdodA==?= MCSE 4 03-02-2006 05:26 PM
.NET 2005 not taking code changes into consideration when building Ryan Ternier ASP .Net 0 01-10-2006 12:39 AM
Is classless worth consideration David MacQuigg Python 33 05-03-2004 05:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57