Noob thread lock question

  • Thread starter Astley Le Jasper
  • Start date
A

Astley Le Jasper

I have a number of threads that write to a database. I have created a
thread lock, but my question is this:

- If one thread hits a lock, do a) all the other threads stop, or b)
just the ones that come to the same lock?
- I presume that the answer is b. In which case do the threads stop
only if they come to the same instance of a lock. For example, you
could have a lock instance for one database and another instance for
another database (first_db_thread_lock = threading.RLock() ....
second_db_thread_lock = threading.RLock()).

I appreciate this is a bit of a noob question, but I didn't want to
assume anything.

ALJ
 
D

Diez B. Roggisch

Astley said:
I have a number of threads that write to a database. I have created a
thread lock, but my question is this:

- If one thread hits a lock, do a) all the other threads stop, or b)
just the ones that come to the same lock?

Only the ones coming the the same lock.
- I presume that the answer is b. In which case do the threads stop
only if they come to the same instance of a lock. For example, you
could have a lock instance for one database and another instance for
another database (first_db_thread_lock = threading.RLock() ....
second_db_thread_lock = threading.RLock()).


There is nothing like "not an instance of a lock". So it's essentially
the same question as the first, and thus the answer is also: yes, only
for the *same* lock, which is an instance.

Diez
 
J

John Nagle

Diez said:
Only the ones coming the the same lock.



There is nothing like "not an instance of a lock". So it's essentially
the same question as the first, and thus the answer is also: yes, only
for the *same* lock, which is an instance.

Diez

Note that if you're using MySQLdb, there are some restrictions on threading.
Only one thread at a time can use each connection to the database. But you
can create multiple connections to the same database at the same time from
a single program, and run them concurrently.

John Nagle
 
L

Lie Ryan

I have a number of threads that write to a database. I have created a
thread lock, but my question is this:

- If one thread hits a lock, do a) all the other threads stop, or b)
just the ones that come to the same lock?

Just the ones the comes to the same lock, but...

Though it should not be a problem since your threads are I/O bound,
don't forget about the GIL (Global Interpeter Lock). A decent DB API
should release the GIL when writing/reading.
 
A

Astley Le Jasper

When you say don't forget about the GIL, what should I not be
forgetting? I'm using sqlite and the following:

<<<<<<<<<<<<<<<code>>>>>>>>>>>>>>>>>>>>>>>>>>

thread_lock = threading.RLock()

def db_execute(sql):

thread_lock.acquire()

try:
connection = sqlite3.connect(database_name)
cursor = connection.cursor()
cursor.execute(sql)
connection.commit()
except:
log.error(formatExceptionInfo())
finally:
thread_lock.release()

<<<<<<<<<<<<<<<code>>>>>>>>>>>>>>>>>>>>>>>>>>
 
L

Lie Ryan

When you say don't forget about the GIL, what should I not be
forgetting? I'm using sqlite and the following:

I mean don't forget that when the GIL is locked, all threads (except the
current one, and threads waiting on I/O) will not be able to run. In
your case, you don't have anything to worry about since you're I/O bound.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top