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 -
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()
-
Thanks a lot,
Luke14free
0 3421 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Robert Zurer |
last post by:
Can anyone suggest the best book or part of a book on this subject. I'm
looking for an in-depth treatment with examples in C#
TIA
Robert Zurer
robert@zurer.com
|
by: Michael Tobis |
last post by:
Someone asked me to write a brief essay regarding the value-add
proposition for Python in the Fortran community. Slightly modified to
remove a few climatology-related specifics, here it is.
I would welcome comments and corrections, and would be happy to
contribute some version of this to the Python website if it is of
interest.
===
|
by: abhinav |
last post by:
Hi guys.I have to implement a topical crawler as a part of my
project.What language should i implement
C or Python?Python though has fast development cycle but my concern is
speed also.I want to strke a balance between development speed and
crawler speed.Since Python is an interpreted language it is rather
slow.The crawler which will be working on huge set of pages should be
as fast as possible.One possible implementation would be...
|
by: abhinav |
last post by:
Hi guys.I have read that one cannot perform true multithreading in
python due to global interpreter lock mechanism.Suppose i have to
implement a crawler on a say cluster system like clusterknoppix so that
i can use parallel virtual machine (PVM)for programming in
multiprocessor environment or say open MPI.Can i integrate python with
PVM or MPI.Can i embed python into C for programming in multiprocessor
environment.Is there any way of...
|
by: Cameron Laird |
last post by:
QOTW: "Dictionaries are one of the most useful things in Python. Make sure
you know how to take adavantage of them..." - Jeremy Sanders
"Python has consistently failed to disappoint me." - Tal Einat
"super() only works on new-style classes ..." and "has its own
set of gotchas":
http://groups.google.com/group/comp.lang.python/msg/f44c8c09e1593dcf
| |
by: =?ISO-8859-2?Q?Krzysztof_W=B3odarczyk?= |
last post by:
Hi,
I think I've found a bug in Python/C API and multithreading.
I'm currently creating an intrusion detection system based on mobile agents.
I have an AgentPlatform (C/C++) and 2 agents on it (2 Python scripts:
Snort and Anomaly)
These 2 agents are each running its own Python interpreter (both by
|
by: joop renes |
last post by:
hi,
i hope this is the right list for the following question of a c++
hacker,python newbie. i have a library in c++ to which i want to add a
python GUI and other python stuff.The library has multithreading
components, while python uses a reference counted memory model. Usually
mixing reference counting with multithreading is frowned upon, at least
in the c++ world. what am i to expect in the python world, if i bring
multithreading c++...
|
by: Gabriel Genellina |
last post by:
En Sun, 04 May 2008 11:56:14 -0300, joop renes <jj.renes@hccnet.nlescribió:
Python objects are reference counted, *and* you can have many threads running. This is not a problem in itself; Python has a Global Interpreter Lock (GIL) that ensures that addref/releases are properly handled. Only one thread at a time can execute Python code; but your C++ library can use as many threads as you want - as long as they don't call Python code again...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |