473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Accessing a database from a multithreaded application

Hi,

I have a problem that is half python, half design. I have a
multithreaded network server working, each client request spawns a new
thread which deals with that client for as long as it is connected
(think ftp style rather than http style connections here). Each thread
gets passed a reference to the main server to access things like the
list of connected clients, global data, etc.

Now I want to add a database to store usernames and other user data. I
grabbed a copy of pysqlite2 which I have used successfully in the past
in single-threaded applications. My initial plan was for the server to
create a single connection and cursor and then just use some sort of
mutex to control access to it from the various threads. However,
apparently you can only access the cursor from the thread on which it
was created...

Can someone suggest a better (ie, valid) strategy for this? Should I
be using a Queue to make a list of db requests/results to get accross
the thread boundary (erg, that sounds nasty)? Should each client
thread create its own connection/cursor to the database? Would that
even work, wont there be locking issues?

Any suggestions or pointers in the direction of more information would
be greatly appreciated.

Thanks for your time,

Alan

Nov 23 '05 #1
2 1655

"Alan Kemp" <go****@alankemp.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi, Can someone suggest a better (ie, valid) strategy for this?
Pass the connection to the thread as a parameter and use it to create a
cursor local to the thread. You may have to create a connection per thread
also - in some database implementations connections are "global" like
cursors are. Cursors are throwaway things anyway.
Should I
be using a Queue to make a list of db requests/results to get accross
the thread boundary (erg, that sounds nasty)?
Maybe - it is safest to limit the amount of threads/cursors/connections that
can be created by people one does not know.
Should each client
thread create its own connection/cursor to the database? Would that
even work, wont there be locking issues?
Yes, and maybe; one connection/cursor for each thread will always work.

Any suggestions or pointers in the direction of more information would
be greatly appreciated.
Maybe use queues to pass the requests to a pool of threads; there might be a
limit to how many connection/cursors that can be created at the same time --
and some DOS tool will find it.

Thanks for your time,

Alan

Nov 23 '05 #2
Alan Kemp schrieb:
I have a problem that is half python, half design. I have a
multithreaded network server working, each client request spawns a new
thread which deals with that client for as long as it is connected
(think ftp style rather than http style connections here). Each thread
gets passed a reference to the main server to access things like the
list of connected clients, global data, etc.
...
Can someone suggest a better (ie, valid) strategy for this?


Have a look at DBUtils (http://www.webwareforpython.org/DBUtils).

Basically, there are two possibilities: Persistent connections that are
bound to your server threads, or a pool of connections that are
independent from the server threads. I prefer the first solution if the
number of server threads stays constant. If the server regularly creates
and destroys threads, I prefer spooling. DBUtils supports both.

I plan to write a doco describing these ideas and the usage of DBUtils
in detail. For now you need to get along with the inline docstrings.

-- Christoph
Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Eric S. Johansson | last post by:
I have an application where I need a very simple database, effectively a very large dictionary. The very large dictionary must be accessed from multiple processes simultaneously. I need to be...
2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
2
by: Ray Mitchell | last post by:
In my main dialog, which opens when my application starts and never closes until the application completes, I have a list box. My assembly also contains several threads. I would like to write to...
14
by: Nick Gilbert | last post by:
Hi, I have an asp.net application which runs from a CD-ROM using Cassini. As such, it is single user only. The application connects to an Access database when it is loaded, and keeps the same...
10
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
6
by: Stephen Carson | last post by:
I'm trying to build a Web Service that will kick off threads as logging requests come in. These threads will then log to the database. I have been able to make a simple Web Service. I have been...
3
by: Jake K | last post by:
I have a multithreaded application that I now want to convert into a Windows Service. Does application.run work in a windows service? Are there things to take into consideration when creating a...
4
by: Noy B | last post by:
Hi, I have developed a small application that is using a MSAccess DB. the problem is that it was developed on a machine where the application and the DB are both located. now it needs to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.