[Tutor] threads

Shitiz Bansal shitizb at yahoo.com
Fri Feb 25 17:17:42 CET 2005


Hi,
Thanx everyone for all your suggestions.I used a
separate thread for every car as I wanted my cars to
run in real time, to coordinate effectively with other
systems like traffic lights for eg. which I cant edit,
hence which cant use the variable time defined by me.

I figured out two ways to achieve this....

On Linux... ulimit -s 64, limits the stacksize of each
thread to 64Kb, and i was able to generate 8000
threads.However upgrading my program to support more
cars was a problem.

Another way was to spawn 1000 threads, handling abt 10
cars each...this way the diff in real time and my
program time was limited.

I tried my hands at Stackless too... but still had
problems implementing the concept.

Can anyone guide me on how to spawn simultaneously( or
pseudo simultaneously) running microthreads using
stackless.

Here is what i tried..

ef gencars(num,origin,dest,speed):
    global adjls
    global cars
    global juncls
    for i in range(num):                              
     
             cars.append(car(orig,dest,speed)
             task=tasklet(cars[i].run())
             task.setup('Bind using Setup')

Now this is what i copied from somewhere...i dont
claim to understand fully what is happening.Here
car.run() is a process which takes a long time to
execute.

What happens on execution is....One car is initialised
and then the program waits for its run method to
complete before proceeding.

I also feel that there is no clear documentation on
stackless.

Show me the light.

Shitiz

         
--- Hugo González Monteverde <hugonz-lists at h-lab.net>
wrote:

> OK, as ignorant as I may be on threads, I have read
> a bit on Stackless 
> Python's tasklets. Suppossedly they require much
> less memory than 
> python's threads.
> 
> Information is at www.stackless.com
> 
> Hugo
> 
> 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.
> > 
> > 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!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list