"Using" is not thread safe.
Would this also happen if i running peewee in a multi-threaded web-server? I'm not familiar with concurrent programming. @chenbo515  I found this case only exists in below scenarios
1) concurrent access to peewee model class.  AND
2) wanna use one peewee model class to access multiple databases which have same schema. You'll need to implement your own locking, unfortunately. Otherwise the `Using` blocks would need to add a lock. Something like this might work:

```python
from contextlib import contextmanager

db_lock = threading.Lock()

@contextmanager
def ConcurrentUsing(*args):
    with db_lock:
        with Using(*args):
            yield
``` @coleifer thank you.
Hi There,

I have a requirement that needs to use one Model to deal with multiple Sqlite db files, and this process may execute concurrently, I did try the "Using" which get from [document]( http://docs.peewee-orm.com/en/latest/peewee/database.html#using-multiple-databases) but found the "Using" is not a thread-safe operation that I cannot confirm which db context will be during the runtime.

May I know whether alternative solution existed?

Fangchih.



sample test code as below:
```

class RawData(BaseModel):
    dir_name = CharField()
    file_name = CharField()
    singer_name = CharField(null=True)
    song_name = CharField()

    class Meta:
        indexes = (
            (('dir_name', 'file_name'), True),
        )


def _insert(db, data):
    print '_insert', db
    with Using(db, [RawData]):
        RawData.insert_many_test(data).execute()


db1 = sqlitedatabase('tmp1.db')
db2 = sqlitedatabase('tmp2.db')

t1 = Thread(target=_insert, args=(db1, song_data[:9]))
t2 = Thread(target=_insert, args=(db2, song_data[:7]))

t1.start()
t2.start()
t1.join()
t2.join()

with Using(db1, [RawData]):
       cnt1 = RawData.select().count()
       self.assertEqual(cnt1, 9)

with Using(self.db2, [RawData]):
       cnt2 = RawData.select().count()
       self.assertEqual(cnt2, 7)

```




