@@ -488,15 +488,41 @@ the ASCII character set.
488488` fitsio ` is a Python wrapper for the ` cfitsio ` library and so inherits the constraints
489489on multithreaded programs from ` cfitsio ` . Specifically this means that
490490
491- - ` fitsio.FITS ` file objects are NOT thread-safe and should never be shared between threads.
492491- Concurrent reading from FITS files is thread-safe, but every thread must open the FITS file
493- on its own, getting unique ` fitsio.FITS ` object.
492+ on its own, getting a unique ` fitsio.FITS ` object.
494493- Concurrent writing to FITS files is NOT thread-safe.
494+ - ` fitsio.FITS ` file objects can be shared between threads for reading, but only one thread
495+ can use the file object at a time and so use needs to be controlled via a lock or some
496+ other mechanism. See the example below.
495497
496498` fitsio ` is compatible with Python free threading, and will not reenable the GIL
497499when imported. However, the constraints above must be respected even when using Python
498500free threading.
499501
502+ Here is an example of using a lock to share a ` fitsio.FITS ` file pointer across threads:
503+
504+ ``` python
505+ import concurrent.futures
506+ import threading
507+ import fitsio
508+
509+
510+ lock = threading.RLock()
511+
512+ def _read_file (fp ):
513+ with lock:
514+ # do something with fp here
515+ pass
516+
517+ with fitsio.FITS(fname) as fp:
518+ with ThreadPoolExecutor(max_workers = 10 ) as exc:
519+ futs = [
520+ exc.submit(_read_file, fp) for _ in range (10 )
521+ ]
522+ for fut in futs:
523+ res = fut.result()
524+ ```
525+
500526## TODO
501527
502528- HDU groups: does anyone use these? If so open an issue!
0 commit comments