473,802 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python multithreading

luke14free
79 New Member
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 3421

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

Similar topics

16
8512
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
53
4393
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. ===
1
3361
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...
1
2348
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...
0
1213
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
3
1575
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
0
888
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++...
0
918
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...
0
9699
marktang
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...
0
9562
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,...
0
10305
jinu1996
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...
1
10285
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,...
0
10063
tracyyun
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...
0
9115
agi2029
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...
0
6838
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();...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.