Shitiz Bansal wrote:
Hi,

I am trying to build a traffic network simulator using
python, for my degree project.

I need to run at least 5-6000 cars simultaneously.I
wanted to run each car in a separate thread.
However , after about 400 threads i am unable to
create new threads.

There does seem to be a limit on how many threads you can create, mostly due to the memory allocated to each thread. Search comp.lang.python for "can't start new thread" - with quotes - or follow the link below to find some discussion.
http://groups-beta.google.com/group/comp.lang.python/search?group=comp.lang.python&q=%22can%27t+start+new+thread%22&qt_g=1&searchnow=Search+this+group


Could you implement your cars as state machines and have a loop that runs through all the cars and gives each one a chance to increment its state?

Do you know about SimPy?
http://simpy.sourceforge.net/

HTH
Kent


Here's the code:

cars=range(1000)
for i in cars:

cars[i]=cars[i]=car(1,10,2,1,adjls,juncls)



for i in cars:

i.start()

Traceback (most recent call last):
  File "<pyshell#24>", line 2, in -toplevel-
    i.start()
error: can't start new thread

Is there a way out.Also, are there any tips on
performance issues?

Here is the class car:

class car(threading.Thread):
def
__init__(self,carid,speed,dest,orig,adjls,juncls):
threading.Thread.__init__(self)
self.speed=speed
self.finished=0
self.carid=carid
self.dest=dest
self.orig=orig
self.adjls=adjls
self.juncls=juncls
self.shortest=find_shortest_path(adjls,self.dest,self.orig)
self.calc=findpaths(adjls,self.dest,self.orig)
self.stats={'currtrsp':0,'avgspeed':0,'distcov':0,'totaltime':0}
def traverse_track(self,p1,p2):
counter=0
time=0
while self.adjls[p1][counter].to!=p2:
counter=counter+1
self.track=self.adjls[p1][counter]
self.pos=0
if self.speed>self.track.speed:
speed=self.track.speed
else:
speed=self.speed
while self.pos!=self.track.length:
if self.track.state.has_key(self.pos):
self.track.state[self.pos].acquire()
else:
self.track.state[self.pos]=threading.Semaphore(value=self.track.lanes)
self.track.state[self.pos].acquire()
if self.pos!=0:
self.track.state[self.pos-1].release()
self.pos=self.pos+1
sleep(1.0/speed)
time=time+1.0/speed
self.stats['currtrsp']=float(self.track.length)/time
if self.stats['avgspeed']:
self.stats['avgspeed']=float(self.stats['distcov']+self.track.length)/(self.stats['distcov']/self.stats['avgspeed']+self.track.length/self.stats['currtrsp'])
else:
self.stats['avgspeed']=self.stats['currtrsp']
self.stats['totaltime']=self.stats['totaltime']+time
if self.track.stats['avgspeed']:
self.track.stats['avgspeed']=(self.track.stats['avgspeed']*self.track.stats['traffictotal']+self.stats['currtrsp'])/(self.track.stats['traffictotal']+1)
else:
self.track.stats['avgspeed']=self.stats['currtrsp']
self.stats['distcov']=self.stats['distcov']+self.track.length
self.track.stats['traffictotal']=self.track.stats['traffictotal']+1
def cross_junction(self,juncls,juncid,orig,dest):
marker=str(orig)+'-'+str(dest)
if juncls[juncid].free.has_key(marker):
self.track.state[self.pos].release()
else:
while not
juncls[juncid].signalled['green'].has_key(marker):
sleep(0.2)
self.track.state[self.pos-1].release()
def run(self):
path=self.shortest
counter=1
for i in path[:1]:
self.traverse_track(i,path[counter])
if not counter==len(path)-1:
self.cross_junction(self.juncls,path[counter],i,path[counter+1])
counter=counter+1
self.finished=1
self.track.state[self.pos-1].release()




__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to