[Tutor] threads
Shitiz Bansal
shitizb at yahoo.com
Wed Feb 23 19:40:52 CET 2005
Hi,
Googling for my problem i found the following page:
http://gnosis.cx/publish/programming/charming_python_b7.html
It doesnt make much sense to me.It seems that the
author is implementing a series of routines rather
than running them sumultaneously as threads are
supposed to do.Am i missing the point?
Shitiz
--- Shitiz Bansal <shitizb at yahoo.com> 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.
>
> 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
__________________________________
Do you Yahoo!?
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
More information about the Tutor
mailing list