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
Thanks a lot,
Luke14free
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
Code:
import random
import threading
import time
import audio #this is a mobile phone library!
# This takes about n/3 seconds to run (about n/3 clumps of tasks, times
# about 1 second per clump).
numtasks = 2
# create a semaphore bounded up to 2
sema = threading.BoundedSemaphore(value=2)
# create a Read Lock
mutex = threading.RLock()
# running is a global variable to keep track
# of how many threads are running
running = 0
itime=int(time.time()[0])
##
##don't worry about those 2 functions, its pys60:) and it should be right!
##
def record(close=False):
try:
open('e:\\opas.wav','w')
filename2 = 'e:\\oprpr.wav'
S2=audio.Sound.open(unicode(filename2))
S2.record()
e32.ao_yield()
except:
1
if close==True:
S2.stop()
S2.close()
def play(close=False):
try:
filename = 'e:\\a.mp3'
S=audio.Sound.open(unicode(filename))
S.record()
e32.ao_yield()
except:
1
if close==True:
S.stop()
S.close()
# the TestThread class is a subclass of threading.Thread,
# so it should supply the standard methods: run, ...
class TestThread(threading.Thread):
def run(self):
global itime
# tell python we access the global variable
global running
delay=15
print 'task', self.getName(), 'will run for', delay, 'sec'
# first, wait on the semaphore (limited to 2 threads)
sema.acquire()
# but only one of these 2 at a time should update
# the running variable
mutex.acquire()
running = running + 1
print running, 'tasks are running'
# release lock so another can update "running"
mutex.release()
if itime==int(time.time()[0])+15:
print 'task', self.getName(), 'done'
# time to decrement "running"
mutex.acquire()
running = running - 1
print self.getName(), 'is finished.', running, 'tasks are running'
mutex.release()
# and finally, exit the group of 2 tasks
sema.release()
print( 'execution terminated' )
elif running==1:
record()
elif running==0:
play()
# after wakeup, say we are done
# main program: build and start all the threads
threads = []
# done in a function just for convenience
def starttasks():
for i in range(numtasks):
# show off Python's formatting feature
# by building a name for each thread
t = TestThread(name="<thread %d>"%i)
# add new name to list
threads.append(t)
# start thread
t.start()
starttasks()
print 'waiting for all tasks to complete'
# next statement waits for all threads to finish
for t in threads: t.join()
Luke14free