Created
June 9, 2016 15:28
-
-
Save alukach/9b61d4812de2e3ccc1ea1889d226ee9e to your computer and use it in GitHub Desktop.
A test script to demonstrate that Twisted was locking up after 30k file deletions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from twisted.internet import inotify, reactor, threads | |
from twisted.python import filepath | |
# Resources: | |
# Introduction to Deferreds: | |
# http://twisted.readthedocs.io/en/twisted-16.1.1/core/howto/defer-intro.html | |
# Returning Deferreds from synchronous functions: | |
# https://twistedmatrix.com/documents/current/core/howto/gendefer.html#returning-deferreds-from-synchronous-functions | |
# addCallbacks Docs: | |
# https://twistedmatrix.com/documents/16.1.1/api/twisted.internet.defer.Deferred.html#addCallbacks | |
# filepath Docs: | |
# http://twistedmatrix.com/documents/current/api/twisted.python.filepath.FilePath.html | |
def file_delete(filepath): | |
""" | |
Some slow deletion that should be run in a separate thread. | |
""" | |
filepath.remove() | |
def errback(failure): | |
print("Failure", failure.getErrorMessage()) | |
def new_file_handler(_, filepath, mask): | |
""" | |
For historical reasons, an opaque handle is passed as first | |
parameter. This object should never be used. | |
@param filepath: FilePath on which the event happened. | |
@param mask: inotify event as hexadecimal masks | |
""" | |
if (mask & inotify.IN_Q_OVERFLOW): | |
sys.exit(1) | |
d = threads.deferToThread(file_delete, filepath) | |
print("Scheduling callback for %s" % filepath.path) | |
d.addErrback(errback) | |
notifier = inotify.INotify() | |
notifier.startReading() | |
notifier.watch(filepath.FilePath('/tmp/bar'), mask=inotify.IN_MODIFY, callbacks=[new_file_handler]) | |
print("Starting watcher...") | |
reactor.run() | |
# To create 10k test files: | |
# $ dd if=/dev/zero of=../masterfile bs=1 count=1000000 && split -b 100 -a 10 /tmp/masterfile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment