Re: File I/O and Multiple Threads
am 22.12.2007 14:00:05 von Anthony Jones"headware"
news:d43971f6-3266-4396-89e8-9aeb3be973fc@x69g2000hsx.google groups.com...
> On Dec 16, 9:31 am, "Anthony Jones"
> > "headware"
> >
> >
news:e502865e-360c-462d-a55c-881df0a5212d@e67g2000hsc.google groups.com...>
On Dec 13, 5:46 pm, "Anthony Jones"
> > > > "headware"
> >
> >
news:5b731448-8b62-4b06-ac5b-777e3ad3f4e5@e23g2000prf.google groups.com...>
> > On Dec 13, 3:28 am, "Anthony Jones"
> "headware"
> >
> >
news:2180e3a5-1b07-4271-ba5e-79ea17e0e0ff@a35g2000prf.google groups.com...
> >
> >
> >
> >
> >
> > > > > > > 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