Keep file descriptor in gen_server. How?

Hello all!

How to keep file descriptor after file:open/2 opened when using it in the state of gen_server?
For example: there are calls that contain information that need to be written to file on which descriptor stored in the state of gen_server?

Either (open it in inside server process (e.g. in init/1)) or (pass it as an argument, but then change the owner via file:change_owner/2|3). Owner of a file is the process that opened it and if it terminates, file gets closed.

Do you mean something like this in init/1:

{ok,IO_DEVICE} = file:open(FILE_PATH,[write]),
file:change_owner(IO_DEVICE,self())

If you open it in init/1 then the server already is the owner, so there is no need to change the owner.

1 Like

Just to keep IO_DEVICE in the state? Is it all is needed?