file write collision consideration

Discussion in 'Python' started by RGK, Jan 20, 2009.

  1. RGK

    RGK Guest

    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.
    RGK, Jan 20, 2009
    #1
    1. Advertising

  2. 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.
    D'Arcy J.M. Cain, Jan 20, 2009
    #2
    1. Advertising

  3. RGK

    MRAB Guest

    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()
    MRAB, Jan 20, 2009
    #3
  4. RGK

    RGK Guest

    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()
    RGK, Jan 20, 2009
    #4
  5. RGK

    Gary Guest

    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?
    Gary, Jan 22, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Ternier
    Replies:
    0
    Views:
    337
    Ryan Ternier
    Jan 10, 2006
  2. David MacQuigg

    Is classless worth consideration

    David MacQuigg, Apr 29, 2004, in forum: Python
    Replies:
    33
    Views:
    862
    Erik Max Francis
    May 3, 2004
  3. steve yee

    about design consideration of strcpy()

    steve yee, May 25, 2006, in forum: C Programming
    Replies:
    13
    Views:
    478
    Charles Richmond
    May 26, 2006
  4. Starman

    Consideration on pointer declarations

    Starman, Mar 5, 2009, in forum: C Programming
    Replies:
    18
    Views:
    590
    JosephKK
    Mar 13, 2009
  5. bpascal123
    Replies:
    6
    Views:
    339
    Keith Thompson
    Jun 6, 2011
Loading...

Share This Page