.. _scardface_and_threads: ===================== SCardFace and Threads ===================== SCardFace is thread-safe, it means that it can be safely implemented using the :mod:`threading` module. In fact, SCardFace already implements several classes from this module. .. _exclusive_access: Exclusive access - design considerations ---------------------------------------- When initialising a :class:`SCard` instance, exclusive access to the card is requested, resulting in having that instance of :class:`SCard` to be the only object interfacing with the smart card. Although one can consider this as a limitation, it has been enforced for the following reasons: 1. Cases where concurrent access to a smart card from different process are rare 2. Concurrent access can still be achieved through threads and using the :keyword:`with` statement 3. Using exclusive access to cards enable more flexibility in the algorithm for initializing the :class:`SCard` instance. It is not possible to establish a smart card connection, when requesting a shared access to the card. .. _using_with_statement: using the :keyword:`with` statement ----------------------------------- The :keyword:`with` stament can be used on :class:`SCard` instances: :: h = SCard( timeout = 30) with h: h.send ( select_application_cmd ) h.send ( get_records_cmd) The :keyword:`with` statement acquires a :class:`threading.Lock` object, and releases it when leaving the block, ensuring that the current thread is the only one to gain access to the smartcard resource. .. note:: If several threads use the :keyword:`with` statement on the same :class:`SCard` object, this will serialize the access and as a consequence block all threads but one. Upon exit of the block, the lock is released.