|
|
The AutoClose parameter, which is on by default, will close all non-shared files opened by an individual interpreter in the group when Ns_TclDeAllocate is invoked. (Other interpreters in the group are not affected.) Ns_TclDeAllocate is called after each Tcl request procedure.
It is recommended that you leave the AutoClose parameter set to on and share files between interpreters.
detach command. A shared file is left open after an interpreter is deallocated when the AutoClose parameter is on (it is on by default). For example: #init time:
set sharedFile [open myfile.dat]
detach $sharedFile
You must manually close the shared file (by performing a close) when it is no longer needed or when the server is shut down.
Note that there is no implicit locking between interpreters. This means that one interpreter can be performing a gets command on the shared file at the same time another interpreter performs a close on the same file. The results are unpredictable, and could potentially cause a core dump. To avoid this situation, use a mutex to protect access to the file whenever it is accessed.
For example:, at initialization time (for example, in your init.tcl script), open a shared file and create a mutex:
set sharedLock [ns_mutex create]
set sharedFile [open myfile.dat]
detach $sharedFile
At run time, use the mutex to protect any actions you perform on the shared file:
global sharedLock
global sharedFile
ns_mutex lock $sharedLock
puts $sharedFile ...
gets $sharedFile ...
ns_mutex unlock $sharedLock
At shutdown (for example, in your shutdown procedure registered with ns_atshutdown), close the file and destroy the lock:
global sharedLock
global sharedFile
close $SharedFile
ns_mutex destroy $SharedLock