File I/O and Multiple Threads

Discussion in 'ASP General' started by headware, Dec 13, 2007.

  1. headware

    headware Guest

    What happens to OpenTextFile if one thread tries to write to the file
    while another thread currently has it open? I tried testing it by
    calling OpenTextFile multiple times without ever calling Close, but it
    still seems to work fine. Is there any way in VBScript that you can
    guarantee that the file stream is closed (something analogous to a
    finally {} block in C#)?

    Dave
     
    headware, Dec 13, 2007
    #1
    1. Advertising

  2. "headware" <> wrote in message
    news:...
    > What happens to OpenTextFile if one thread tries to write to the file
    > while another thread currently has it open? I tried testing it by
    > calling OpenTextFile multiple times without ever calling Close, but it
    > still seems to work fine. Is there any way in VBScript that you can
    > guarantee that the file stream is closed (something analogous to a
    > finally {} block in C#)?



    Objects in VBScript use deterministic finalisation. IOW when a variable
    containing an object reference is re-assigned or passes out of scope
    (because execution has exited the function it was declared in) that object
    reference is released synchoronously. When the last reference to a
    TextStream object is released it will close its file.



    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 13, 2007
    #2
    1. Advertising

  3. headware

    headware Guest

    On Dec 13, 3:28 am, "Anthony Jones" <> wrote:
    > "headware" <> wrote in message
    >
    > news:...
    >
    > > What happens to OpenTextFile if one thread tries to write to the file
    > > while another thread currently has it open? I tried testing it by
    > > calling OpenTextFile multiple times without ever calling Close, but it
    > > still seems to work fine. Is there any way in VBScript that you can
    > > guarantee that the file stream is closed (something analogous to a
    > > finally {} block in C#)?

    >
    > Objects in VBScript use deterministic finalisation. IOW when a variable
    > containing an object reference is re-assigned or passes out of scope
    > (because execution has exited the function it was declared in) that object
    > reference is released synchoronously. When the last reference to a
    > TextStream object is released it will close its file.
    >
    > --
    > Anthony Jones - MVP ASP/ASP.NET


    Thanks for the reply Anthony. So VBScript allows multiple threads to
    hold write streams to the same file (which is not something you can do
    in .NET), but the WriteLine method must synchronize access to the file
    right? Otherwise you would get multiple threads writing to the file at
    the same time and the output would be mangled. I'm having difficulty
    finding documentation on the thread-safety of the file I/O code in
    VBScript.
     
    headware, Dec 13, 2007
    #3
  4. "headware" <> wrote in message
    news:...
    > On Dec 13, 3:28 am, "Anthony Jones" <> wrote:
    > > "headware" <> wrote in message
    > >
    > >

    news:...
    > >
    > > > What happens to OpenTextFile if one thread tries to write to the file
    > > > while another thread currently has it open? I tried testing it by
    > > > calling OpenTextFile multiple times without ever calling Close, but it
    > > > still seems to work fine. Is there any way in VBScript that you can
    > > > guarantee that the file stream is closed (something analogous to a
    > > > finally {} block in C#)?

    > >
    > > Objects in VBScript use deterministic finalisation. IOW when a variable
    > > containing an object reference is re-assigned or passes out of scope
    > > (because execution has exited the function it was declared in) that

    object
    > > reference is released synchoronously. When the last reference to a
    > > TextStream object is released it will close its file.
    > >
    > > --
    > > Anthony Jones - MVP ASP/ASP.NET

    >
    > Thanks for the reply Anthony. So VBScript allows multiple threads to
    > hold write streams to the same file (which is not something you can do
    > in .NET), but the WriteLine method must synchronize access to the file
    > right? Otherwise you would get multiple threads writing to the file at
    > the same time and the output would be mangled. I'm having difficulty
    > finding documentation on the thread-safety of the file I/O code in
    > VBScript.


    This isn't really a threadsafety issue. You could have two processes
    sharing file, even two processes running on different machines sharing the
    file.

    If they all have write access to the file and attempt to write to the file
    it is quite likely to get mangled.


    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 13, 2007
    #4
  5. headware

    headware Guest

    On Dec 13, 5:46 pm, "Anthony Jones" <> wrote:
    > "headware" <> wrote in message
    >
    > news:...> On Dec 13, 3:28 am, "Anthony Jones" <> wrote:
    > > > "headware" <> wrote in message

    >
    > news:...
    >
    >
    >
    >
    >
    > > > > What happens to OpenTextFile if one thread tries to write to the file
    > > > > while another thread currently has it open? I tried testing it by
    > > > > calling OpenTextFile multiple times without ever calling Close, but it
    > > > > still seems to work fine. Is there any way in VBScript that you can
    > > > > guarantee that the file stream is closed (something analogous to a
    > > > > finally {} block in C#)?

    >
    > > > Objects in VBScript use deterministic finalisation. IOW when a variable
    > > > containing an object reference is re-assigned or passes out of scope
    > > > (because execution has exited the function it was declared in) that

    > object
    > > > reference is released synchoronously. When the last reference to a
    > > > TextStream object is released it will close its file.

    >
    > > > --
    > > > Anthony Jones - MVP ASP/ASP.NET

    >
    > > Thanks for the reply Anthony. So VBScript allows multiple threads to
    > > hold write streams to the same file (which is not something you can do
    > > in .NET), but the WriteLine method must synchronize access to the file
    > > right? Otherwise you would get multiple threads writing to the file at
    > > the same time and the output would be mangled. I'm having difficulty
    > > finding documentation on the thread-safety of the file I/O code in
    > > VBScript.

    >
    > This isn't really a threadsafety issue. You could have two processes
    > sharing file, even two processes running on different machines sharing the
    > file.
    >
    > If they all have write access to the file and attempt to write to the file
    > it is quite likely to get mangled.
    >
    > --
    > Anthony Jones - MVP ASP/ASP.NET


    Well, it's a thread safety issue in the regard that you could have
    more than one thread accessing shared data (the file) simultaneously.

    The background here is that I have an ASP app that is using a log file
    for errors. I'm worried that if more than one request (it's my
    understanding that each request in IIS is run under its own thread, at
    least for IIS 5) were to write to the log at the same time, it would
    cause problems. I was first concerned that if one thread tried to
    write to the log file while another had it open that it would error
    out since this is what happens in .NET. It's clear now that this does
    not actually happen, but it sounds like there still is the possibility
    of the logging output being interleaved between more than one thread
    making the log unreadable.

    I haven't seen any way to perform thread synchronization in VBScript,
    so I'm not sure if this is a problem that can be easily fixed or if
    I'm even thinking about it in the right way.

    Dave
     
    headware, Dec 16, 2007
    #5
  6. "headware" <> wrote in message
    news:...
    > On Dec 13, 5:46 pm, "Anthony Jones" <> wrote:
    > > "headware" <> wrote in message
    > >
    > >

    news:...>
    On Dec 13, 3:28 am, "Anthony Jones" <> wrote:
    > > > > "headware" <> wrote in message

    > >
    > >

    news:...
    > >
    > >
    > >
    > >
    > >
    > > > > > What happens to OpenTextFile if one thread tries to write to the

    file
    > > > > > while another thread currently has it open? I tried testing it by
    > > > > > calling OpenTextFile multiple times without ever calling Close,

    but it
    > > > > > still seems to work fine. Is there any way in VBScript that you

    can
    > > > > > guarantee that the file stream is closed (something analogous to a
    > > > > > finally {} block in C#)?

    > >
    > > > > Objects in VBScript use deterministic finalisation. IOW when a

    variable
    > > > > containing an object reference is re-assigned or passes out of scope
    > > > > (because execution has exited the function it was declared in) that

    > > object
    > > > > reference is released synchoronously. When the last reference to a
    > > > > TextStream object is released it will close its file.

    > >
    > > > > --
    > > > > Anthony Jones - MVP ASP/ASP.NET

    > >
    > > > Thanks for the reply Anthony. So VBScript allows multiple threads to
    > > > hold write streams to the same file (which is not something you can do
    > > > in .NET), but the WriteLine method must synchronize access to the file
    > > > right? Otherwise you would get multiple threads writing to the file at
    > > > the same time and the output would be mangled. I'm having difficulty
    > > > finding documentation on the thread-safety of the file I/O code in
    > > > VBScript.

    > >
    > > This isn't really a threadsafety issue. You could have two processes
    > > sharing file, even two processes running on different machines sharing

    the
    > > file.
    > >
    > > If they all have write access to the file and attempt to write to the

    file
    > > it is quite likely to get mangled.
    > >
    > > --
    > > Anthony Jones - MVP ASP/ASP.NET

    >
    > Well, it's a thread safety issue in the regard that you could have
    > more than one thread accessing shared data (the file) simultaneously.
    >


    Typically we use the term 'Thread safety' to refer to issues related to code
    in a process modifying memory locations simultaneously (since all threads in
    a process share the same Virtual memory space) resulting in unexpected
    behaviour.

    > The background here is that I have an ASP app that is using a log file
    > for errors. I'm worried that if more than one request (it's my
    > understanding that each request in IIS is run under its own thread, at
    > least for IIS 5) were to write to the log at the same time, it would
    > cause problems.


    Yes multiple request can be processed simultaneously in different threads.

    > I was first concerned that if one thread tried to
    > write to the log file while another had it open that it would error
    > out since this is what happens in .NET. It's clear now that this does
    > not actually happen, but it sounds like there still is the possibility
    > of the logging output being interleaved between more than one thread
    > making the log unreadable.


    I'm not sure how you tested this but a call to OpenTextFile will throw a
    permission denied if you attempt to open for write or append a file already
    open elsewhere. In you C# version were you discarding and/or closing the
    log file? In C# it would be much easier to have a singleton log file object
    that serialised calls write a log.

    >
    > I haven't seen any way to perform thread synchronization in VBScript,
    > so I'm not sure if this is a problem that can be easily fixed or if
    > I'm even thinking about it in the right way.
    >


    There is no builtin way to synchronize ASP threads.


    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 16, 2007
    #6
  7. headware

    headware Guest

    On Dec 16, 9:31 am, "Anthony Jones" <> wrote:
    > "headware" <> wrote in message
    >
    > news:...> On Dec 13, 5:46 pm, "Anthony Jones" <> wrote:
    > > > "headware" <> wrote in message

    >
    > news:...>
    > On Dec 13, 3:28 am, "Anthony Jones" <> wrote:> > > > "headware" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > > > > > What happens to OpenTextFile if one thread tries to write to the

    > file
    > > > > > > while another thread currently has it open? I tried testing it by
    > > > > > > calling OpenTextFile multiple times without ever calling Close,

    > but it
    > > > > > > still seems to work fine. Is there any way in VBScript that you

    > can
    > > > > > > guarantee that the file stream is closed (something analogous to a
    > > > > > > finally {} block in C#)?

    >
    > > > > > Objects in VBScript use deterministic finalisation. IOW when a

    > variable
    > > > > > containing an object reference is re-assigned or passes out of scope
    > > > > > (because execution has exited the function it was declared in) that
    > > > object
    > > > > > reference is released synchoronously. When the last reference to a
    > > > > > TextStream object is released it will close its file.

    >
    > > > > > --
    > > > > > Anthony Jones - MVP ASP/ASP.NET

    >
    > > > > Thanks for the reply Anthony. So VBScript allows multiple threads to
    > > > > hold write streams to the same file (which is not something you can do
    > > > > in .NET), but the WriteLine method must synchronize access to the file
    > > > > right? Otherwise you would get multiple threads writing to the file at
    > > > > the same time and the output would be mangled. I'm having difficulty
    > > > > finding documentation on the thread-safety of the file I/O code in
    > > > > VBScript.

    >
    > > > This isn't really a threadsafety issue. You could have two processes
    > > > sharing file, even two processes running on different machines sharing

    > the
    > > > file.

    >
    > > > If they all have write access to the file and attempt to write to the

    > file
    > > > it is quite likely to get mangled.

    >
    > > > --
    > > > Anthony Jones - MVP ASP/ASP.NET

    >
    > > Well, it's a thread safety issue in the regard that you could have
    > > more than one thread accessing shared data (the file) simultaneously.

    >
    > Typically we use the term 'Thread safety' to refer to issues related to code
    > in a process modifying memory locations simultaneously (since all threads in
    > a process share the same Virtual memory space) resulting in unexpected
    > behaviour.
    >
    > > The background here is that I have an ASP app that is using a log file
    > > for errors. I'm worried that if more than one request (it's my
    > > understanding that each request in IIS is run under its own thread, at
    > > least for IIS 5) were to write to the log at the same time, it would
    > > cause problems.

    >
    > Yes multiple request can be processed simultaneously in different threads.
    >
    > > I was first concerned that if one thread tried to
    > > write to the log file while another had it open that it would error
    > > out since this is what happens in .NET. It's clear now that this does
    > > not actually happen, but it sounds like there still is the possibility
    > > of the logging output being interleaved between more than one thread
    > > making the log unreadable.

    >
    > I'm not sure how you tested this but a call to OpenTextFile will throw a
    > permission denied if you attempt to open for write or append a file already
    > open elsewhere. In you C# version were you discarding and/or closing the
    > log file? In C# it would be much easier to have a singleton log file object
    > that serialised calls write a log.
    >
    >
    >
    > > I haven't seen any way to perform thread synchronization in VBScript,
    > > so I'm not sure if this is a problem that can be easily fixed or if
    > > I'm even thinking about it in the right way.

    >
    > There is no builtin way to synchronize ASP threads.
    >
    > --
    > Anthony Jones - MVP ASP/ASP.NET


    All I did to test it was to open the log file in one thread and not
    close it. I realize now though, after reading your first post, that
    such as test won't work since the log file is closed by the system
    when the reference goes out of scope. I suppose I would have to force
    the thread to wait while the reference is still in scope and the file
    is still open while the other thread comes in and tries to open it in
    order to recreate the permissions error you mentioned.

    I only mentioned the C# code as a reference for something that worked
    in a way I understood. I was able to write C# code that gave me an
    error when multiple threads tired to write to the file; but couldn't
    do that in VBScript (now I know why). The app I'm working on is a
    classic ASP web application, by the way.

    The way I would solve this problem is C#, like you suggested, would be
    to have a logging class that synchronized thread access to the file.
    Is there a way to do something like this in VBScript? How do people
    normally deal with logging in classic ASP?

    Dave
     
    headware, Dec 17, 2007
    #7
  8. "headware" <> wrote in message
    news:...
    > On Dec 16, 9:31 am, "Anthony Jones" <> wrote:
    > > "headware" <> wrote in message
    > >
    > >

    news:...>
    On Dec 13, 5:46 pm, "Anthony Jones" <> wrote:
    > > > > "headware" <> wrote in message

    > >
    > >

    news:...>
    > > On Dec 13, 3:28 am, "Anthony Jones" <> wrote:> > >

    > "headware" <> wrote in message
    > >
    > >

    news:...
    > >
    > >
    > >
    > >
    > >
    > > > > > > > What happens to OpenTextFile if one thread tries to write to

    the
    > > file
    > > > > > > > while another thread currently has it open? I tried testing it

    by
    > > > > > > > calling OpenTextFile multiple times without ever calling

    Close,
    > > but it
    > > > > > > > still seems to work fine. Is there any way in VBScript that

    you
    > > can
    > > > > > > > guarantee that the file stream is closed (something analogous

    to a
    > > > > > > > finally {} block in C#)?

    > >
    > > > > > > Objects in VBScript use deterministic finalisation. IOW when a

    > > variable
    > > > > > > containing an object reference is re-assigned or passes out of

    scope
    > > > > > > (because execution has exited the function it was declared in)

    that
    > > > > object
    > > > > > > reference is released synchoronously. When the last reference

    to a
    > > > > > > TextStream object is released it will close its file.

    > >
    > > > > > > --
    > > > > > > Anthony Jones - MVP ASP/ASP.NET

    > >
    > > > > > Thanks for the reply Anthony. So VBScript allows multiple threads

    to
    > > > > > hold write streams to the same file (which is not something you

    can do
    > > > > > in .NET), but the WriteLine method must synchronize access to the

    file
    > > > > > right? Otherwise you would get multiple threads writing to the

    file at
    > > > > > the same time and the output would be mangled. I'm having

    difficulty
    > > > > > finding documentation on the thread-safety of the file I/O code in
    > > > > > VBScript.

    > >
    > > > > This isn't really a threadsafety issue. You could have two

    processes
    > > > > sharing file, even two processes running on different machines

    sharing
    > > the
    > > > > file.

    > >
    > > > > If they all have write access to the file and attempt to write to

    the
    > > file
    > > > > it is quite likely to get mangled.

    > >
    > > > > --
    > > > > Anthony Jones - MVP ASP/ASP.NET

    > >
    > > > Well, it's a thread safety issue in the regard that you could have
    > > > more than one thread accessing shared data (the file) simultaneously.

    > >
    > > Typically we use the term 'Thread safety' to refer to issues related to

    code
    > > in a process modifying memory locations simultaneously (since all

    threads in
    > > a process share the same Virtual memory space) resulting in unexpected
    > > behaviour.
    > >
    > > > The background here is that I have an ASP app that is using a log file
    > > > for errors. I'm worried that if more than one request (it's my
    > > > understanding that each request in IIS is run under its own thread, at
    > > > least for IIS 5) were to write to the log at the same time, it would
    > > > cause problems.

    > >
    > > Yes multiple request can be processed simultaneously in different

    threads.
    > >
    > > > I was first concerned that if one thread tried to
    > > > write to the log file while another had it open that it would error
    > > > out since this is what happens in .NET. It's clear now that this does
    > > > not actually happen, but it sounds like there still is the possibility
    > > > of the logging output being interleaved between more than one thread
    > > > making the log unreadable.

    > >
    > > I'm not sure how you tested this but a call to OpenTextFile will throw a
    > > permission denied if you attempt to open for write or append a file

    already
    > > open elsewhere. In you C# version were you discarding and/or closing

    the
    > > log file? In C# it would be much easier to have a singleton log file

    object
    > > that serialised calls write a log.
    > >
    > >
    > >
    > > > I haven't seen any way to perform thread synchronization in VBScript,
    > > > so I'm not sure if this is a problem that can be easily fixed or if
    > > > I'm even thinking about it in the right way.

    > >
    > > There is no builtin way to synchronize ASP threads.
    > >
    > > --
    > > Anthony Jones - MVP ASP/ASP.NET

    >
    > All I did to test it was to open the log file in one thread and not
    > close it. I realize now though, after reading your first post, that
    > such as test won't work since the log file is closed by the system
    > when the reference goes out of scope. I suppose I would have to force
    > the thread to wait while the reference is still in scope and the file
    > is still open while the other thread comes in and tries to open it in
    > order to recreate the permissions error you mentioned.
    >
    > I only mentioned the C# code as a reference for something that worked
    > in a way I understood. I was able to write C# code that gave me an
    > error when multiple threads tired to write to the file; but couldn't
    > do that in VBScript (now I know why). The app I'm working on is a
    > classic ASP web application, by the way.
    >
    > The way I would solve this problem is C#, like you suggested, would be
    > to have a logging class that synchronized thread access to the file.
    > Is there a way to do something like this in VBScript? How do people
    > normally deal with logging in classic ASP?
    >


    The best approach from a purely VBScript in ASP point of view is to use a
    database as a means of logging. The DB therefore handles the simultaneous
    entries in the log.


    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Dec 22, 2007
    #8
    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. Smegly
    Replies:
    1
    Views:
    1,147
    Mitchell
    May 19, 2004
  2. yoda
    Replies:
    2
    Views:
    478
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    446
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,519
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    184
    Une bévue
    Jun 14, 2006
Loading...

Share This Page