Revert "Use NamedTemporaryFile instead of mkstemp"

This reverts commit 170ada228b3cdfcc84910fb30a3822bb13f65f01.
Revert "Close open file handles; use mkstemp and write with 'wb'"

This reverts commit 5519388b387cee15fde46ac4ee162adae1715486.
Attempt to fix a 'too many open file handles' issue