kqueue.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
  5. # Copyright 2012 Google, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. """
  19. :module: watchdog.observers.kqueue
  20. :synopsis: ``kqueue(2)`` based emitter implementation.
  21. :author: yesudeep@google.com (Yesudeep Mangalapilly)
  22. :platforms: Mac OS X and BSD with kqueue(2).
  23. .. WARNING:: kqueue is a very heavyweight way to monitor file systems.
  24. Each kqueue-detected directory modification triggers
  25. a full directory scan. Traversing the entire directory tree
  26. and opening file descriptors for all files will create
  27. performance problems. We need to find a way to re-scan
  28. only those directories which report changes and do a diff
  29. between two sub-DirectorySnapshots perhaps.
  30. .. ADMONITION:: About OS X performance guidelines
  31. Quote from the `Mac OS X File System Performance Guidelines`_:
  32. "When you only want to track changes on a file or directory, be sure to
  33. open it using the ``O_EVTONLY`` flag. This flag prevents the file or
  34. directory from being marked as open or in use. This is important
  35. if you are tracking files on a removable volume and the user tries to
  36. unmount the volume. With this flag in place, the system knows it can
  37. dismiss the volume. If you had opened the files or directories without
  38. this flag, the volume would be marked as busy and would not be
  39. unmounted."
  40. ``O_EVTONLY`` is defined as ``0x8000`` in the OS X header files.
  41. More information here: http://www.mlsite.net/blog/?p=2312
  42. Classes
  43. -------
  44. .. autoclass:: KqueueEmitter
  45. :members:
  46. :show-inheritance:
  47. Collections and Utility Classes
  48. -------------------------------
  49. .. autoclass:: KeventDescriptor
  50. :members:
  51. :show-inheritance:
  52. .. autoclass:: KeventDescriptorSet
  53. :members:
  54. :show-inheritance:
  55. .. _Mac OS X File System Performance Guidelines:
  56. http://developer.apple.com/library/ios/#documentation/Performance/Conceptual/FileSystem/Articles/TrackingChanges.html#//apple_ref/doc/uid/20001993-CJBJFIDD
  57. """
  58. from __future__ import with_statement
  59. from watchdog.utils import platform
  60. import threading
  61. import errno
  62. import stat
  63. import os
  64. import select
  65. from pathtools.path import absolute_path
  66. from watchdog.observers.api import (
  67. BaseObserver,
  68. EventEmitter,
  69. DEFAULT_OBSERVER_TIMEOUT,
  70. DEFAULT_EMITTER_TIMEOUT
  71. )
  72. from watchdog.utils.dirsnapshot import DirectorySnapshot
  73. from watchdog.events import (
  74. DirMovedEvent,
  75. DirDeletedEvent,
  76. DirCreatedEvent,
  77. DirModifiedEvent,
  78. FileMovedEvent,
  79. FileDeletedEvent,
  80. FileCreatedEvent,
  81. FileModifiedEvent,
  82. EVENT_TYPE_MOVED,
  83. EVENT_TYPE_DELETED,
  84. EVENT_TYPE_CREATED
  85. )
  86. # Maximum number of events to process.
  87. MAX_EVENTS = 4096
  88. # O_EVTONLY value from the header files for OS X only.
  89. O_EVTONLY = 0x8000
  90. # Pre-calculated values for the kevent filter, flags, and fflags attributes.
  91. if platform.is_darwin():
  92. WATCHDOG_OS_OPEN_FLAGS = O_EVTONLY
  93. else:
  94. WATCHDOG_OS_OPEN_FLAGS = os.O_RDONLY | os.O_NONBLOCK
  95. WATCHDOG_KQ_FILTER = select.KQ_FILTER_VNODE
  96. WATCHDOG_KQ_EV_FLAGS = select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR
  97. WATCHDOG_KQ_FFLAGS = (
  98. select.KQ_NOTE_DELETE
  99. | select.KQ_NOTE_WRITE
  100. | select.KQ_NOTE_EXTEND
  101. | select.KQ_NOTE_ATTRIB
  102. | select.KQ_NOTE_LINK
  103. | select.KQ_NOTE_RENAME
  104. | select.KQ_NOTE_REVOKE
  105. )
  106. # Flag tests.
  107. def is_deleted(kev):
  108. """Determines whether the given kevent represents deletion."""
  109. return kev.fflags & select.KQ_NOTE_DELETE
  110. def is_modified(kev):
  111. """Determines whether the given kevent represents modification."""
  112. fflags = kev.fflags
  113. return (fflags & select.KQ_NOTE_EXTEND) or (fflags & select.KQ_NOTE_WRITE)
  114. def is_attrib_modified(kev):
  115. """Determines whether the given kevent represents attribute modification."""
  116. return kev.fflags & select.KQ_NOTE_ATTRIB
  117. def is_renamed(kev):
  118. """Determines whether the given kevent represents movement."""
  119. return kev.fflags & select.KQ_NOTE_RENAME
  120. class KeventDescriptorSet(object):
  121. """
  122. Thread-safe kevent descriptor collection.
  123. """
  124. def __init__(self):
  125. # Set of KeventDescriptor
  126. self._descriptors = set()
  127. # Descriptor for a given path.
  128. self._descriptor_for_path = dict()
  129. # Descriptor for a given fd.
  130. self._descriptor_for_fd = dict()
  131. # List of kevent objects.
  132. self._kevents = list()
  133. self._lock = threading.Lock()
  134. @property
  135. def kevents(self):
  136. """
  137. List of kevents monitored.
  138. """
  139. with self._lock:
  140. return self._kevents
  141. @property
  142. def paths(self):
  143. """
  144. List of paths for which kevents have been created.
  145. """
  146. with self._lock:
  147. return list(self._descriptor_for_path.keys())
  148. def get_for_fd(self, fd):
  149. """
  150. Given a file descriptor, returns the kevent descriptor object
  151. for it.
  152. :param fd:
  153. OS file descriptor.
  154. :type fd:
  155. ``int``
  156. :returns:
  157. A :class:`KeventDescriptor` object.
  158. """
  159. with self._lock:
  160. return self._descriptor_for_fd[fd]
  161. def get(self, path):
  162. """
  163. Obtains a :class:`KeventDescriptor` object for the specified path.
  164. :param path:
  165. Path for which the descriptor will be obtained.
  166. """
  167. with self._lock:
  168. path = absolute_path(path)
  169. return self._get(path)
  170. def __contains__(self, path):
  171. """
  172. Determines whether a :class:`KeventDescriptor has been registered
  173. for the specified path.
  174. :param path:
  175. Path for which the descriptor will be obtained.
  176. """
  177. with self._lock:
  178. path = absolute_path(path)
  179. return self._has_path(path)
  180. def add(self, path, is_directory):
  181. """
  182. Adds a :class:`KeventDescriptor` to the collection for the given
  183. path.
  184. :param path:
  185. The path for which a :class:`KeventDescriptor` object will be
  186. added.
  187. :param is_directory:
  188. ``True`` if the path refers to a directory; ``False`` otherwise.
  189. :type is_directory:
  190. ``bool``
  191. """
  192. with self._lock:
  193. path = absolute_path(path)
  194. if not self._has_path(path):
  195. self._add_descriptor(KeventDescriptor(path, is_directory))
  196. def remove(self, path):
  197. """
  198. Removes the :class:`KeventDescriptor` object for the given path
  199. if it already exists.
  200. :param path:
  201. Path for which the :class:`KeventDescriptor` object will be
  202. removed.
  203. """
  204. with self._lock:
  205. path = absolute_path(path)
  206. if self._has_path(path):
  207. self._remove_descriptor(self._get(path))
  208. def clear(self):
  209. """
  210. Clears the collection and closes all open descriptors.
  211. """
  212. with self._lock:
  213. for descriptor in self._descriptors:
  214. descriptor.close()
  215. self._descriptors.clear()
  216. self._descriptor_for_fd.clear()
  217. self._descriptor_for_path.clear()
  218. self._kevents = []
  219. # Thread-unsafe methods. Locking is provided at a higher level.
  220. def _get(self, path):
  221. """Returns a kevent descriptor for a given path."""
  222. return self._descriptor_for_path[path]
  223. def _has_path(self, path):
  224. """Determines whether a :class:`KeventDescriptor` for the specified
  225. path exists already in the collection."""
  226. return path in self._descriptor_for_path
  227. def _add_descriptor(self, descriptor):
  228. """
  229. Adds a descriptor to the collection.
  230. :param descriptor:
  231. An instance of :class:`KeventDescriptor` to be added.
  232. """
  233. self._descriptors.add(descriptor)
  234. self._kevents.append(descriptor.kevent)
  235. self._descriptor_for_path[descriptor.path] = descriptor
  236. self._descriptor_for_fd[descriptor.fd] = descriptor
  237. def _remove_descriptor(self, descriptor):
  238. """
  239. Removes a descriptor from the collection.
  240. :param descriptor:
  241. An instance of :class:`KeventDescriptor` to be removed.
  242. """
  243. self._descriptors.remove(descriptor)
  244. del self._descriptor_for_fd[descriptor.fd]
  245. del self._descriptor_for_path[descriptor.path]
  246. self._kevents.remove(descriptor.kevent)
  247. descriptor.close()
  248. class KeventDescriptor(object):
  249. """
  250. A kevent descriptor convenience data structure to keep together:
  251. * kevent
  252. * directory status
  253. * path
  254. * file descriptor
  255. :param path:
  256. Path string for which a kevent descriptor will be created.
  257. :param is_directory:
  258. ``True`` if the path refers to a directory; ``False`` otherwise.
  259. :type is_directory:
  260. ``bool``
  261. """
  262. def __init__(self, path, is_directory):
  263. self._path = absolute_path(path)
  264. self._is_directory = is_directory
  265. self._fd = os.open(path, WATCHDOG_OS_OPEN_FLAGS)
  266. self._kev = select.kevent(self._fd,
  267. filter=WATCHDOG_KQ_FILTER,
  268. flags=WATCHDOG_KQ_EV_FLAGS,
  269. fflags=WATCHDOG_KQ_FFLAGS)
  270. @property
  271. def fd(self):
  272. """OS file descriptor for the kevent descriptor."""
  273. return self._fd
  274. @property
  275. def path(self):
  276. """The path associated with the kevent descriptor."""
  277. return self._path
  278. @property
  279. def kevent(self):
  280. """The kevent object associated with the kevent descriptor."""
  281. return self._kev
  282. @property
  283. def is_directory(self):
  284. """Determines whether the kevent descriptor refers to a directory.
  285. :returns:
  286. ``True`` or ``False``
  287. """
  288. return self._is_directory
  289. def close(self):
  290. """
  291. Closes the file descriptor associated with a kevent descriptor.
  292. """
  293. try:
  294. os.close(self.fd)
  295. except OSError:
  296. pass
  297. @property
  298. def key(self):
  299. return (self.path, self.is_directory)
  300. def __eq__(self, descriptor):
  301. return self.key == descriptor.key
  302. def __ne__(self, descriptor):
  303. return self.key != descriptor.key
  304. def __hash__(self):
  305. return hash(self.key)
  306. def __repr__(self):
  307. return "<%s: path=%s, is_directory=%s>"\
  308. % (type(self).__name__, self.path, self.is_directory)
  309. class KqueueEmitter(EventEmitter):
  310. """
  311. kqueue(2)-based event emitter.
  312. .. ADMONITION:: About ``kqueue(2)`` behavior and this implementation
  313. ``kqueue(2)`` monitors file system events only for
  314. open descriptors, which means, this emitter does a lot of
  315. book-keeping behind the scenes to keep track of open
  316. descriptors for every entry in the monitored directory tree.
  317. This also means the number of maximum open file descriptors
  318. on your system must be increased **manually**.
  319. Usually, issuing a call to ``ulimit`` should suffice::
  320. ulimit -n 1024
  321. Ensure that you pick a number that is larger than the
  322. number of files you expect to be monitored.
  323. ``kqueue(2)`` does not provide enough information about the
  324. following things:
  325. * The destination path of a file or directory that is renamed.
  326. * Creation of a file or directory within a directory; in this
  327. case, ``kqueue(2)`` only indicates a modified event on the
  328. parent directory.
  329. Therefore, this emitter takes a snapshot of the directory
  330. tree when ``kqueue(2)`` detects a change on the file system
  331. to be able to determine the above information.
  332. :param event_queue:
  333. The event queue to fill with events.
  334. :param watch:
  335. A watch object representing the directory to monitor.
  336. :type watch:
  337. :class:`watchdog.observers.api.ObservedWatch`
  338. :param timeout:
  339. Read events blocking timeout (in seconds).
  340. :type timeout:
  341. ``float``
  342. """
  343. def __init__(self, event_queue, watch, timeout=DEFAULT_EMITTER_TIMEOUT):
  344. EventEmitter.__init__(self, event_queue, watch, timeout)
  345. self._kq = select.kqueue()
  346. self._lock = threading.RLock()
  347. # A collection of KeventDescriptor.
  348. self._descriptors = KeventDescriptorSet()
  349. def walker_callback(path, stat_info, self=self):
  350. self._register_kevent(path, stat.S_ISDIR(stat_info.st_mode))
  351. self._snapshot = DirectorySnapshot(watch.path,
  352. watch.is_recursive,
  353. walker_callback)
  354. def _register_kevent(self, path, is_directory):
  355. """
  356. Registers a kevent descriptor for the given path.
  357. :param path:
  358. Path for which a kevent descriptor will be created.
  359. :param is_directory:
  360. ``True`` if the path refers to a directory; ``False`` otherwise.
  361. :type is_directory:
  362. ``bool``
  363. """
  364. try:
  365. self._descriptors.add(path, is_directory)
  366. except OSError as e:
  367. if e.errno == errno.ENOENT:
  368. # Probably dealing with a temporary file that was created
  369. # and then quickly deleted before we could open
  370. # a descriptor for it. Therefore, simply queue a sequence
  371. # of created and deleted events for the path.
  372. # path = absolute_path(path)
  373. # if is_directory:
  374. # self.queue_event(DirCreatedEvent(path))
  375. # self.queue_event(DirDeletedEvent(path))
  376. # else:
  377. # self.queue_event(FileCreatedEvent(path))
  378. # self.queue_event(FileDeletedEvent(path))
  379. # TODO: We could simply ignore these files.
  380. # Locked files cause the python process to die with
  381. # a bus error when we handle temporary files.
  382. # eg. .git/index.lock when running tig operations.
  383. # I don't fully understand this at the moment.
  384. pass
  385. else:
  386. # All other errors are propagated.
  387. raise
  388. def _unregister_kevent(self, path):
  389. """
  390. Convenience function to close the kevent descriptor for a
  391. specified kqueue-monitored path.
  392. :param path:
  393. Path for which the kevent descriptor will be closed.
  394. """
  395. self._descriptors.remove(path)
  396. def queue_event(self, event):
  397. """
  398. Handles queueing a single event object.
  399. :param event:
  400. An instance of :class:`watchdog.events.FileSystemEvent`
  401. or a subclass.
  402. """
  403. # Handles all the book keeping for queued events.
  404. # We do not need to fire moved/deleted events for all subitems in
  405. # a directory tree here, because this function is called by kqueue
  406. # for all those events anyway.
  407. EventEmitter.queue_event(self, event)
  408. if event.event_type == EVENT_TYPE_CREATED:
  409. self._register_kevent(event.src_path, event.is_directory)
  410. elif event.event_type == EVENT_TYPE_MOVED:
  411. self._unregister_kevent(event.src_path)
  412. self._register_kevent(event.dest_path, event.is_directory)
  413. elif event.event_type == EVENT_TYPE_DELETED:
  414. self._unregister_kevent(event.src_path)
  415. def _queue_dirs_modified(self,
  416. dirs_modified,
  417. ref_snapshot,
  418. new_snapshot):
  419. """
  420. Queues events for directory modifications by scanning the directory
  421. for changes.
  422. A scan is a comparison between two snapshots of the same directory
  423. taken at two different times. This also determines whether files
  424. or directories were created, which updated the modified timestamp
  425. for the directory.
  426. """
  427. if dirs_modified:
  428. for dir_modified in dirs_modified:
  429. self.queue_event(DirModifiedEvent(dir_modified))
  430. diff_events = new_snapshot - ref_snapshot
  431. for file_created in diff_events.files_created:
  432. self.queue_event(FileCreatedEvent(file_created))
  433. for directory_created in diff_events.dirs_created:
  434. self.queue_event(DirCreatedEvent(directory_created))
  435. def _queue_events_except_renames_and_dir_modifications(self, event_list):
  436. """
  437. Queues events from the kevent list returned from the call to
  438. :meth:`select.kqueue.control`.
  439. .. NOTE:: Queues only the deletions, file modifications,
  440. attribute modifications. The other events, namely,
  441. file creation, directory modification, file rename,
  442. directory rename, directory creation, etc. are
  443. determined by comparing directory snapshots.
  444. """
  445. files_renamed = set()
  446. dirs_renamed = set()
  447. dirs_modified = set()
  448. for kev in event_list:
  449. descriptor = self._descriptors.get_for_fd(kev.ident)
  450. src_path = descriptor.path
  451. if is_deleted(kev):
  452. if descriptor.is_directory:
  453. self.queue_event(DirDeletedEvent(src_path))
  454. else:
  455. self.queue_event(FileDeletedEvent(src_path))
  456. elif is_attrib_modified(kev):
  457. if descriptor.is_directory:
  458. self.queue_event(DirModifiedEvent(src_path))
  459. else:
  460. self.queue_event(FileModifiedEvent(src_path))
  461. elif is_modified(kev):
  462. if descriptor.is_directory:
  463. # When a directory is modified, it may be due to
  464. # sub-file/directory renames or new file/directory
  465. # creation. We determine all this by comparing
  466. # snapshots later.
  467. dirs_modified.add(src_path)
  468. else:
  469. self.queue_event(FileModifiedEvent(src_path))
  470. elif is_renamed(kev):
  471. # Kqueue does not specify the destination names for renames
  472. # to, so we have to process these after taking a snapshot
  473. # of the directory.
  474. if descriptor.is_directory:
  475. dirs_renamed.add(src_path)
  476. else:
  477. files_renamed.add(src_path)
  478. return files_renamed, dirs_renamed, dirs_modified
  479. def _queue_renamed(self,
  480. src_path,
  481. is_directory,
  482. ref_snapshot,
  483. new_snapshot):
  484. """
  485. Compares information from two directory snapshots (one taken before
  486. the rename operation and another taken right after) to determine the
  487. destination path of the file system object renamed, and adds
  488. appropriate events to the event queue.
  489. """
  490. try:
  491. ref_stat_info = ref_snapshot.stat_info(src_path)
  492. except KeyError:
  493. # Probably caught a temporary file/directory that was renamed
  494. # and deleted. Fires a sequence of created and deleted events
  495. # for the path.
  496. if is_directory:
  497. self.queue_event(DirCreatedEvent(src_path))
  498. self.queue_event(DirDeletedEvent(src_path))
  499. else:
  500. self.queue_event(FileCreatedEvent(src_path))
  501. self.queue_event(FileDeletedEvent(src_path))
  502. # We don't process any further and bail out assuming
  503. # the event represents deletion/creation instead of movement.
  504. return
  505. try:
  506. dest_path = absolute_path(
  507. new_snapshot.path(ref_stat_info.st_ino))
  508. if is_directory:
  509. event = DirMovedEvent(src_path, dest_path)
  510. # TODO: Do we need to fire moved events for the items
  511. # inside the directory tree? Does kqueue does this
  512. # all by itself? Check this and then enable this code
  513. # only if it doesn't already.
  514. # A: It doesn't. So I've enabled this block.
  515. if self.watch.is_recursive:
  516. for sub_event in event.sub_moved_events():
  517. self.queue_event(sub_event)
  518. self.queue_event(event)
  519. else:
  520. self.queue_event(FileMovedEvent(src_path, dest_path))
  521. except KeyError:
  522. # If the new snapshot does not have an inode for the
  523. # old path, we haven't found the new name. Therefore,
  524. # we mark it as deleted and remove unregister the path.
  525. if is_directory:
  526. self.queue_event(DirDeletedEvent(src_path))
  527. else:
  528. self.queue_event(FileDeletedEvent(src_path))
  529. def _read_events(self, timeout=None):
  530. """
  531. Reads events from a call to the blocking
  532. :meth:`select.kqueue.control()` method.
  533. :param timeout:
  534. Blocking timeout for reading events.
  535. :type timeout:
  536. ``float`` (seconds)
  537. """
  538. return self._kq.control(self._descriptors.kevents,
  539. MAX_EVENTS,
  540. timeout)
  541. def queue_events(self, timeout):
  542. """
  543. Queues events by reading them from a call to the blocking
  544. :meth:`select.kqueue.control()` method.
  545. :param timeout:
  546. Blocking timeout for reading events.
  547. :type timeout:
  548. ``float`` (seconds)
  549. """
  550. with self._lock:
  551. try:
  552. event_list = self._read_events(timeout)
  553. files_renamed, dirs_renamed, dirs_modified = (
  554. self._queue_events_except_renames_and_dir_modifications(event_list))
  555. # Take a fresh snapshot of the directory and update the
  556. # saved snapshot.
  557. new_snapshot = DirectorySnapshot(self.watch.path,
  558. self.watch.is_recursive)
  559. ref_snapshot = self._snapshot
  560. self._snapshot = new_snapshot
  561. if files_renamed or dirs_renamed or dirs_modified:
  562. for src_path in files_renamed:
  563. self._queue_renamed(src_path,
  564. False,
  565. ref_snapshot,
  566. new_snapshot)
  567. for src_path in dirs_renamed:
  568. self._queue_renamed(src_path,
  569. True,
  570. ref_snapshot,
  571. new_snapshot)
  572. self._queue_dirs_modified(dirs_modified,
  573. ref_snapshot,
  574. new_snapshot)
  575. except OSError as e:
  576. if e.errno == errno.EBADF:
  577. # logging.debug(e)
  578. pass
  579. else:
  580. raise
  581. def on_thread_stop(self):
  582. # Clean up.
  583. with self._lock:
  584. self._descriptors.clear()
  585. self._kq.close()
  586. class KqueueObserver(BaseObserver):
  587. """
  588. Observer thread that schedules watching directories and dispatches
  589. calls to event handlers.
  590. """
  591. def __init__(self, timeout=DEFAULT_OBSERVER_TIMEOUT):
  592. BaseObserver.__init__(self, emitter_class=KqueueEmitter, timeout=timeout)