SCardFace and Threads

SCardFace is thread-safe, it means that it can be safely implemented using the threading module.

In fact, SCardFace already implements several classes from this module.

Exclusive access - design considerations

When initialising a SCard instance, exclusive access to the card is requested, resulting in having that instance of 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 with statement
  3. Using exclusive access to cards enable more flexibility in the algorithm for initializing the SCard instance.

It is not possible to establish a smart card connection, when requesting a shared access to the card.

using the with statement

The with stament can be used on SCard instances:

h = SCard( timeout = 30)

with h:
     h.send ( select_application_cmd )
     h.send ( get_records_cmd)

The with statement acquires a 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 with statement on the same 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.

Table Of Contents

Previous topic

Introduction and Tutorial

Next topic

Dealing with readers hot insertion and removal

This Page