buckket / twtxt

Decentralised, minimalist microblogging service for hackers.
http://twtxt.readthedocs.org/en/stable/
MIT License
1.92k stars 79 forks source link

Implement atomic write for tweets #37

Closed timofurrer closed 8 years ago

timofurrer commented 8 years ago

Implement atomic writing for the local tweets.

Refs #34

buckket commented 8 years ago

Click already has a custom open_file() method which allows atomic behaviour: http://click.pocoo.org/6/api/#click.open_file.

Maybe we can make use of it.

timofurrer commented 8 years ago

I'll have a look at clicks open_file.

timofurrer commented 8 years ago

click does not support atomic appending. See https://github.com/mitsuhiko/click/blob/master/click/_compat.py#L434 They suggest to open in w-mode and copy data explicitly.. What do you think about that?

kitchen commented 8 years ago

Yea, that's basically what you have to do. Copy the file, append to new file, rename back.

-Jeremy

Insert PGP signature here.

On Feb 8, 2016, at 9:46 AM, Timo Furrer notifications@github.com wrote:

click does not support atomic appending. See https://github.com/mitsuhiko/click/blob/master/click/_compat.py#L434 They suggest to open in w-mode and copy data explicitly.. What do you think about that?

— Reply to this email directly or view it on GitHub.

timofurrer commented 8 years ago

I've updated the PR. You can easily test it with this:

>>> with twtxt.helper.atomic_write("testfile", "a") as f:
...     f.write("Hallo")
...     raise AttributeError("FAILED")
... 
5
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AttributeError: FAILED
>>> 
timofurrer commented 8 years ago

@kitchen do you agree with this implementation?