473,698 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading - race condition?

i'm getting the wrong output for the 'title' attributes for this
data. the queue holds a data structure (item name, position, and list
to store results in). each thread takes in an item name and queries a
database for various attributes. from the debug statements the item
names are being retrieved correctly, but the attributes returned are
those of other items in the queue - not its own item. however, the
model variable is not a global variable... so i'm not sure what's
wrong.

i've declared a bunch of worker threads (100) and a queue into which
new requests are inserted, like so:

queue = Queue.Queue(0)
WORKERS=100
for i in range(WORKERS):
thread = SDBThread(queue )
thread.setDaemo n(True)
thread.start()

the thread:

class SimpleDBThread ( threading.Threa d ):
def __init__ ( self, queue ):
self.__queue = queue
threading.Threa d.__init__ ( self )
def run ( self ):
while 1:
item = self.__queue.ge t()
if item!=None:
model = domain.get_item (item[0])
logger.debug('s dbthread item:'+item[0])
title = model['title']
scraped = model['scraped']
logger.debug("s dbthread title:"+title)

any suggestions?
thanks
Jun 27 '08 #1
5 2525
skunkwerk wrote:
i'm getting the wrong output for the 'title' attributes for this
data. the queue holds a data structure (item name, position, and list
to store results in). each thread takes in an item name and queries a
database for various attributes. from the debug statements the item
names are being retrieved correctly, but the attributes returned are
those of other items in the queue - not its own item. however, the
model variable is not a global variable... so i'm not sure what's
wrong.

i've declared a bunch of worker threads (100) and a queue into which
new requests are inserted, like so:

queue = Queue.Queue(0)
WORKERS=100
for i in range(WORKERS):
thread = SDBThread(queue )
thread.setDaemo n(True)
thread.start()

the thread:

class SimpleDBThread ( threading.Threa d ):
def __init__ ( self, queue ):
self.__queue = queue
threading.Threa d.__init__ ( self )
def run ( self ):
while 1:
item = self.__queue.ge t()
if item!=None:
model = domain.get_item (item[0])
logger.debug('s dbthread item:'+item[0])
title = model['title']
scraped = model['scraped']
logger.debug("s dbthread title:"+title)

any suggestions?
thanks
Hm. We don't have enough code here to see what's wrong.
For one thing, we're not seeing how items get put on the queue. The
trouble might be at the "put" end.

Make sure that "model", "item", "title", and "scraped" are not globals.
Remember, any assignment to them in a global context makes them a global.

You should never get "None" from the queue unless you put a "None"
on the queue. "get()" blocks until there's work to do.

John Nagle
Jun 27 '08 #2
On May 9, 12:12*am, John Nagle <na...@animats. comwrote:
skunkwerk wrote:
i'm getting the wrong output for the 'title' attributes for this
data. *the queue holds a data structure (item name, position, and list
to store results in). *each thread takes in an item name and queries a
database for various attributes. *from the debug statements the item
names are being retrieved correctly, but the attributes returned are
those of other items in the queue - not its own item. *however, the
model variable is not a global variable... so i'm not sure what's
wrong.
i've declared a bunch of workerthreads(1 00) and a queue into which
new requests are inserted, like so:
queue = Queue.Queue(0)
*WORKERS=100
for i in range(WORKERS):
* *thread = SDBThread(queue )
* *thread.setDaem on(True)
* *thread.start()
the thread:
class SimpleDBThread ( threading.Threa d ):
* *def __init__ ( self, queue ):
* * * * * *self.__queue = queue
* * * * * *threading.Thre ad.__init__ ( self )
* *def run ( self ):
* * * * * *while 1:
* * * * * * * * * *item = self.__queue.ge t()
* * * * * * * * * *if item!=None:
* * * * * * * * * * * * * *model = domain.get_item (item[0])
* * * * * * * * * * * * * *logger.debug(' sdbthread item:'+item[0])
* * * * * * * * * * * * * *title = model['title']
* * * * * * * * * * * * * *scraped = model['scraped']
* * * * * * * * * * * * * *logger.debug(" sdbthread title:"+title)
any suggestions?
thanks

* *Hm. *We don't have enough code here to see what's wrong.
For one thing, we're not seeing how items get put on the queue. *The
trouble might be at the "put" end.

* *Make sure that "model", "item", "title", and "scraped" are not globals.
Remember, any assignment to them in a global context makes them a global.

* *You should never get "None" from the queue unless you put a "None"
on the queue. *"get()" blocks until there's work to do.

* * * * * * * * * * * * * * * * * * * * John Nagle
thanks John, Gabriel,
here's the 'put' side of the requests:

def prepSDBSearch(r esults):
modelList = [0]
counter=1
for result in results:
data = [result.item, counter, modelList]
queue.put(data)
counter+=1
while modelList[0] < len(results):
print 'waiting...'#wa it for them to come home
modelList.pop(0 )#now remove '0'
return modelList

responses to your follow ups:
1) 'item' in the threads is a list that corresponds to the 'data'
list in the above function. it's not global, and the initial values
seem ok, but i'm not sure if every time i pass in data to the queue it
passes in the same memory address or declares a new 'data' list (which
I guess is what I want)
2) john, i don't think any of the variables you mentioned are
global. the 'none' check was just for extra safety.
3) the first item in the modelList is a counter that keeps track of
the number of threads for this call that have completed - is there any
better way of doing this?

thanks again
Jun 27 '08 #3
En Sun, 11 May 2008 13:16:25 -0300, skunkwerk <sk*******@gmai l.comescribió:
the only issue i have now is that it takes a long time for 100 threads
to initialize that connection (>5 minutes) - and as i'm doing this on
a webserver any time i update the code i have to restart all those
threads, which i'm doing right now in a for loop. is there any way I
can keep the thread stuff separate from the rest of the code for this
file, yet allow access?
Like using a separate thread to create the other 100?

--
Gabriel Genellina

Jun 27 '08 #4
On May 11, 1:55 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
On Sun, 11 May 2008 09:16:25 -0700 (PDT),skunkwerk
<skunkw...@gmai l.comdeclaimed the following in comp.lang.pytho n:
the only issue i have now is that it takes a long time for 100 threads
to initialize that connection (>5 minutes) - and as i'm doing this on
a webserver any time i update the code i have to restart all those
threads, which i'm doing right now in a for loop. is there any way I
can keep the thread stuff separate from the rest of the code for this
file, yet allow access? It wouldn't help having a .pyc or using
psycho, correct, as the time is being spent in the runtime? something
along the lines of 'start a new thread every minute until you get to a
100' without blocking the execution of the rest of the code in that
file? or maybe any time i need to do a search, start a new thread if
the #threads is <100?

Is this running as part of the server process, or as a client
accessing the server?

Alternative question: Have you tried measuring the performance using
/fewer/ threads... 25 or less? I believe I'd mentioned prior that you
seem to have a lot of overhead code for what may be a short query.

If the .get_item() code is doing a full sequence of: connect to
database; format&submit query; fetch results; disconnect from
database... I'd recommend putting the connect/disconnect outside of the
thread while loop (though you may then need to put sentinel values into
the feed queue -- one per thread -- so they can cleanly exit and
disconnect rather than relying on daemonization for exit).

thread:
dbcon = ...
while True:
query = Q.get()
if query == SENTINEL: break
result = get_item(dbcon, query)
...
dbcon.close()

Third alternative: Find some way to combine the database queries.
Rather than 100 threads each doing a single lookup (from your code, it
appears that only 1 result is expected per search term), run 10 threads
each looking up 10 items at once...

thread:
dbcon = ...
terms = []
terminate = False
while not terminate:
while len(terms) < 10:
query = Q.get_nowait()
if not query: break
if query == SENTINEL:
terminate = True
break
terms.append(qu ery)
results = get_item(dbcon, terms)
terms = []
#however you are returning items; match the query term to the
#key item in the list of returned data?
dbcon.close()

where the final select statement looks something like:

SQL = """select key, title, scraped from ***
where key in ( %s )""" % ", ".join("?" for x in terms)
#assumes database adapter uses ? for placeholder
dbcur.execute(S QL, terms)
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netc om.com wulfr...@bestia ria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria. com)
HTTP://www.bestiaria.com/
thanks again Dennis,
i chose 100 threads so i could do 10 simultaneous searches (where
each search contains 10 terms - using 10 threads). the .get_item()
code is not doing the database connection - rather the intialization
is done in the initialization of each thread. so basically once a
thread starts the database connection is persistent and .get_item
queries are very fast. this is running as a server process (using
django).

cheers
Jun 27 '08 #5
On May 11, 9:10 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sun, 11 May 2008 13:16:25 -0300,skunkwerk< skunkw...@gmail .comescribió:
the only issue i have now is that it takes a long time for 100 threads
to initialize that connection (>5 minutes) - and as i'm doing this on
a webserver any time i update the code i have to restart all those
threads, which i'm doing right now in a for loop. is there any way I
can keep the thread stuff separate from the rest of the code for this
file, yet allow access?

Like using a separate thread to create the other 100?

--
Gabriel Genellina
thanks Gabriel,
i think that could do it - let me try it out. don't know why i
didn't think of it earlier.
Jun 27 '08 #6

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

Similar topics

0
1324
by: Mark English | last post by:
Every once in a while since I moved to Python 2.4 I've been seeing the following exception in threading.py Condition: File "mctest3.py", line 1598, in WaitForMessages self.condResponses.wait(1.0) File "C:\Program Files\Python24\lib\threading.py", line 221, in wait delay = min(delay * 2, remaining, .05) OverflowError: long int too large to convert to int Is there something I'm doing wrong here ? I've looked at my code, and
3
2443
by: Juergen Stein | last post by:
Hi Group, I couldn't find an answer on this with Google, so let me test you :) I've a fairly complex WebApp, and I put most of the JS code in independent external .js files. One of these external files contains a class A. The inline JS then inherits a class B from A. My question is now, since the class A is defined in the external file, is the class A definitely known at the point where I use it? I could
2
3609
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in order to solve the folowing problem: We´re working on a server side application that implements threads (pthread under linux Red Hat ES 2.1 kernel 2.4.9). The problem seems
0
2396
by: Richard | last post by:
Hi, I'm suffering a socket race condition - I think. The code works fine at full speed on a single CPU machine. However when I run full speed on a 2 Zeon machine the socket drops data on an asynchronous receive. If I slow a connected client down by step debugging or putting Thread.Sleep() calls between sends then the code works on the Zeon machine. Below is pseudo code that removes all error checking, try catch, business logic, and...
5
4136
by: anonymous | last post by:
I'm writing a program that deals extensively with the printer. For the most part my application runs fine, but occasionally I run into some Exceptions. The most common exceptions I run into are NullReferenceException and InvalidOperationException : The object is currently in use elsewhere. Unfortunately this appears to be a race condition. These exceptions appear to ocurr while disposing GDI+ objects, the Graphics object seems to be the...
3
2970
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also for Item (this)? Normally,do I need sync at all? Isn't that true without sync the program can run faster?
2
2025
by: antonyliu2002 | last post by:
I do not quite understand the race condition. As I posted a couple of days ago, I create a PDF on the fly in my web application at regular intervals. Users will be able to download the PDF file. Suppose, at the time when my application is in the process of generating the PDF file (which takes around 2.5 minutes), a user tries to download it, will this be a problem?
2
1954
by: ian.cross | last post by:
Hi everyone, If a multi-threaded .NET program creates a race condition updating a List (for example), could this cause a memory leak/overwritten memory/ access to another objects' private memory? Basically I'd like to know if sloppy multi-threaded code can break the CLR's guarantees of 'managed code' - garbage collection, bounds-checking, type safety etc? First time poster, long time reader - be gentle.
7
2305
by: Berryl Hesh | last post by:
I wouldn't nomally post this here, as it has something to do with the ListView control usage I think, or maybe with a race condition or some windoes messaging. I'm just not sure. The test below succeeds IF i walk through the debugger and IF I examine the ListViewItem's properties in the debugger visualizer. Otherwise, it fails. Any ideas? Thanks. BH
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.