Ensuring files are closed promptly
Tag : file , By : Kilimanjaro
Date : March 29 2020, 07:55 AM
will help you Consider using a package such as conduit, pipes, iteratee or enumerator. They provide much of the benefits of lazy IO (simpler code, potentially smaller memory footprint) without the lazy IO. Here's an example using conduit and cereal: import Data.Conduit
import Data.Conduit.Binary (sinkFile, sourceFile)
import Data.Conduit.Cereal (sinkGet, sourcePut)
import Data.Conduit.Zlib (gzip, ungzip)
import Data.Serialize (Serialize, get, put)
encodeAndCompressFile :: Serialize a => FilePath -> a -> IO ()
encodeAndCompressFile f v =
runResourceT $ sourcePut (put v) $$ gzip =$ sinkFile f
decodeAndDecompressFile :: Serialize a => FilePath -> IO a
decodeAndDecompressFile f = do
val <- runResourceT $ sourceFile f $$ ungzip =$ sinkGet get
case val of
Right v -> return v
Left err -> fail err
main = do
let i = 0 :: Int
encodeAndCompressFile "test.dat" i
doStuff
doStuff = do
i <- decodeAndDecompressFile "test.dat" :: IO Int
print i
encodeAndCompressFile "test.dat" (i+1)
doStuff
|
Ensuring files are closed in Python
Tag : python , By : Frank Bradley
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use with to open files. When you use with, the file will be implicitly closed when the with block is exited, and it will handle exception states as well.
|
Ensuring my cursor is closed
Date : March 29 2020, 07:55 AM
Does that help I ended up just making the cursor a global and calling cursor.close() in an onDestroy() @Override.
|
difficulty ensuring old records from a database get deleted when a certain form is closed
Tag : vb.net , By : Hitesh Prajapati
Date : March 29 2020, 07:55 AM
it helps some times Your idea of the server pinging the client is a good one. But it can be reversed. For example, by using a TCP connection, you can send the username or ID of the player to the server every two minutes through the client. If the player's client has not pinged the server in two minutes, the server can assume that the player is offline and delete the old 'match' instead of the client. You can run the server dedicated and it will always work well. For more information on TCP data communication, just google it!
|
What is the preferred method of ensuring that database objects are closed?
Tag : java , By : phatfish
Date : March 29 2020, 07:55 AM
To fix this issue If you're using Java 6 or prior, then use the latter because it's easier to read and maintain. Note that the latter can be improved with some refactoring to handle the cumbersome try-catch for every call to close method. If you're using Java 7 or higher, then use try-with-resources: try (Connection con = ...;
PreparedStatement pstmt = ...) {
pstmt.setXyz(...);
ResultSet rs = pstmt.executeQuery();
//read data from resultset
//and then close it
rs.close();
} catch (Exception e) {
//handle the exception properly...
}
try (Connection con = ...;
PreparedStatement pstmt = ...) {
pstmt.setXyz(...);
try(ResultSet rs = pstmt.executeQuery()) {
//read data from resultset
}
} catch (Exception e) {
//handle the exception properly...
}
|