468,287 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,287 developers. It's quick & easy.

python multithreading

luke14free
Hello,
I was trying to run a multithreading example with python on my phone that lets me to record and play at the same time.I've found an example somewhere but, as expected it doesnt work,,,could someone please correct it?
I think it's full of errors or I've really wrong everything...:D
Expand|Select|Wrap|Line Numbers
  1. import random
  2. import threading
  3. import time
  4. import audio #this is a mobile phone library!
  5.  
  6. # This takes about n/3 seconds to run (about n/3 clumps of tasks, times
  7. # about 1 second per clump).
  8. numtasks = 2
  9.  
  10. # create a semaphore bounded up to 2
  11. sema = threading.BoundedSemaphore(value=2)
  12.  
  13. # create a Read Lock
  14. mutex = threading.RLock()
  15.  
  16. # running is a global variable to keep track
  17. # of how many threads are running
  18. running = 0
  19. itime=int(time.time()[0])
  20. ##
  21. ##don't worry about those 2 functions, its pys60:) and it should be right!
  22. ##
  23. def record(close=False):
  24.     try:
  25.         open('e:\\opas.wav','w')
  26.         filename2 = 'e:\\oprpr.wav'
  27.         S2=audio.Sound.open(unicode(filename2))
  28.         S2.record()
  29.         e32.ao_yield()
  30.     except:
  31.         1
  32.     if close==True:
  33.         S2.stop()
  34.         S2.close()
  35. def play(close=False):
  36.     try:
  37.         filename = 'e:\\a.mp3'
  38.         S=audio.Sound.open(unicode(filename))
  39.         S.record()
  40.         e32.ao_yield()
  41.     except:
  42.         1
  43.     if close==True:
  44.         S.stop()
  45.         S.close()
  46. # the TestThread class is a subclass of threading.Thread,
  47. # so it should supply the standard methods: run, ...
  48.  
  49. class TestThread(threading.Thread):
  50.     def run(self):
  51.         global itime
  52.         # tell python we access the global variable 
  53.         global running
  54.         delay=15
  55.         print 'task', self.getName(), 'will run for', delay, 'sec'
  56.         # first, wait on the semaphore (limited to 2 threads)
  57.         sema.acquire()
  58.         # but only one of these 2 at a time should update
  59.         # the running variable 
  60.         mutex.acquire()
  61.         running = running + 1
  62.         print running, 'tasks are running'
  63.         # release lock so another can update "running"
  64.         mutex.release()
  65.         if itime==int(time.time()[0])+15:
  66.             print 'task', self.getName(), 'done'
  67.             # time to decrement "running"
  68.             mutex.acquire()
  69.             running = running - 1
  70.             print self.getName(), 'is finished.', running, 'tasks are running'
  71.             mutex.release()
  72.             # and finally, exit the group of 2 tasks
  73.             sema.release()
  74.             print( 'execution terminated' )
  75.         elif running==1:
  76.             record()
  77.         elif running==0:
  78.             play()
  79.         # after wakeup, say we are done
  80.  
  81.  
  82. # main program:  build and start all the threads
  83. threads = []
  84.  
  85. # done in a function just for convenience
  86. def starttasks():
  87.     for i in range(numtasks):
  88.         # show off Python's formatting feature
  89.         # by building a name for each thread
  90.         t = TestThread(name="<thread %d>"%i)
  91.         # add new name to list
  92.         threads.append(t)
  93.         # start thread
  94.         t.start()
  95.  
  96. starttasks()
  97.  
  98. print 'waiting for all tasks to complete'
  99. # next statement waits for all threads to finish
  100. for t in threads: t.join()
  101.  
Thanks a lot,
Luke14free
May 14 '07 #1
0 3255

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Robert Zurer | last post: by
53 posts views Thread by Michael Tobis | last post: by
1 post views Thread by abhinav | last post: by
reply views Thread by Cameron Laird | last post: by
3 posts views Thread by =?ISO-8859-2?Q?Krzysztof_W=B3odarczyk?= | last post: by
reply views Thread by joop renes | last post: by
reply views Thread by Gabriel Genellina | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.