473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading troubles

Hi folks

What am I doing wrong in the following? I just want to run fluidsynth in
the background.
############### ############### ###########
class MyThread(thread ing.Thread):
def __init__(self, cmd, callback):
self.__cmd = cmd
self.__callback = callback
threading.Threa d.__init__(self )

def run(self):
os.system(self. __cmd)
self.__callback ('abcd')
return
cmd=midiplay+' '+fmidi
xc=MyThread(cmd ,addlog)
xc.start()
############### #######
midiplay is 'fluidsynth -ni /home/mysndfont.sf2 mymidi.mid'
addlog is a function which prints the log.

If I run it only with xc.start() it does not run the program as in
os.system. However if I put
xc.start()
xc.run()

then it starts and runs it in foreground with my pygtk ui non responsive.

What am I missing!

Thanks for any ideas
sree
Jul 10 '06 #1
9 1373
you don't need twisted to run processes in the background, either.
os.popen* returns a file or set of files representing std streams
immediately
subprocess.Pope n is a spiffy little object new in 2.4 and available for
download for 2.3. check the module docstrings for usage tips.

you can use threads, but try doing it the python way instead of the
java way. ;-)
def func(cmd, callback):
os.system(cmd)
callback()
xc = threading.Threa d(target=func, args=(cmd, callback))
xc.start()
sreekant wrote:
Hi folks

What am I doing wrong in the following? I just want to run fluidsynth in
the background.
############### ############### ###########
class MyThread(thread ing.Thread):
def __init__(self, cmd, callback):
self.__cmd = cmd
self.__callback = callback
threading.Threa d.__init__(self )

def run(self):
os.system(self. __cmd)
self.__callback ('abcd')
return
cmd=midiplay+' '+fmidi
xc=MyThread(cmd ,addlog)
xc.start()
############### #######
midiplay is 'fluidsynth -ni /home/mysndfont.sf2 mymidi.mid'
addlog is a function which prints the log.

If I run it only with xc.start() it does not run the program as in
os.system. However if I put
xc.start()
xc.run()

then it starts and runs it in foreground with my pygtk ui non responsive.

What am I missing!

Thanks for any ideas
sree
Jul 10 '06 #2
Hi there

I tried as advised. Now the function gets called only after I hit quit
button which calls gtk.main_quit() at which point, the ui stays on but
not responsive, while the fluidsynth runs in the fg, then ui disappears
as the fluidsynth finishes and presumably the thread dies.
xc = threading.Threa d(target=player ,args=(midicmd, fmidi,addlog))
xc.start()

def player(mp,fm,ca llback):
res=os.system(m p+' '+fm)
os.remove(fm)
return

I tried in the player, both os.popen3 and os.system and os.popen3 with
cmd+' &' and the same with os.system . But all do the same thing.

Any ideas!
Ta
sree
>
you can use threads, but try doing it the python way instead of the
java way. ;-)
def func(cmd, callback):
os.system(cmd)
callback()
xc = threading.Threa d(target=func, args=(cmd, callback))
xc.start()

Jul 10 '06 #3
sreekant wrote:
Hi folks

What am I doing wrong in the following? I just want to run fluidsynth in
the background.
Others have pointed you at os.popen. In non-crippled languages, use
processes (e.g. popen) when you wish to preserve the years of hard work
that OS designers put into protected memory. Use threads only when you
know why you want to abandon such and share all memory. 95% or more
of the time when you're making the multiprocessing decision, threads
are the wrong choice. 5% (or less) of the time they're indispensable.
But if you're just looking for asychronous processing and don't know
why you need to abandon memory protection, go with processes.

Jul 11 '06 #4
>>>>sreekant <sk*****@lithiu m.com(S) wrote:
>SHi folks
SWhat am I doing wrong in the following? I just want to run fluidsynth in
Sthe background.
S############# ############### #############
Sclass MyThread(thread ing.Thread):
S def __init__(self, cmd, callback):
S self.__cmd = cmd
S self.__callback = callback
S threading.Threa d.__init__(self )
>S def run(self):
S os.system(self. __cmd)
S self.__callback ('abcd')
S return
>Scmd=midiplay+ ' '+fmidi
Sxc=MyThread(c md,addlog)
Sxc.start()
>S############# #########
Smidiplay is 'fluidsynth -ni /home/mysndfont.sf2 mymidi.mid'
Saddlog is a function which prints the log.
>SIf I run it only with xc.start() it does not run the program as in
Sos.system. However if I put
Sxc.start()
Sxc.run()
>Sthen it starts and runs it in foreground with my pygtk ui non responsive.
You shouldn't call run() yourself. It is for the thread library itself.
I tried your example with cmd='ls -l' and it works. If I put a print
'started' after the xc.start(), it prints that before the output of ls -l,
indicating that ls -l is running in the background. (Of course the actual
order is up to the scheduler.) Maybe running it inside a pygtk program
could be different but I wouldn't know why.
--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
Private email: pi**@vanoostrum .org
Jul 11 '06 #5
I decided in the end to use fork and all is well.

Thanks
sree
Jul 11 '06 #6
>>>>sreekant <sk*****@lithiu m.com(S) wrote:
>SI decided in the end to use fork and all is well.
But how are you doing the callback then? From your code it looks like the
callback is called after the external command finishes. The callback would
then be called in the child process, not in the parent process, I think. Or
do you have a solution for that?
--
Piet van Oostrum <pi**@cs.uu.n l>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
Private email: pi**@vanoostrum .org
Jul 11 '06 #7
Piet van Oostrum wrote:
>>>>>sreekant <sk*****@lithiu m.com(S) wrote:
>SI decided in the end to use fork and all is well.

But how are you doing the callback then? From your code it looks like the
callback is called after the external command finishes. The callback would
then be called in the child process, not in the parent process, I think. Or
do you have a solution for that?
I am not calling the callback now.

It is a pygtk interface to octavia music description language I wrote. I
have a button "Play" which reads the text in the gtk.TextView , converts
it to midi and calls fluidsynth with the resulting midi file.

I want the ui to be accessible during the midi play because, the midi
play can some times be a long piece of music.

I also have a log window which is another textview. I was loading the
result of

res=os.popen3(m y_command)
report=res[1].read()+'\n'+re s[2].read()
logwindow gets the report added to it.

However now I just put a message saying "playing midi"

I don't know any way out at the moment.

############### #

What I need is when I click on the play button, the fileplay(widget )
function that gets called should be able to start a new thread or a fork
which executes the commands and updates the ui.logwindow which is a
textview with the output of os.popen3.

During the execution the ui should be usable.

Please see the scala program in this package
http://sourceforge.net/projects/octavia . Checkout fileplay() function
in the most latest version 0.22 .

I don't want to attach the whole program to this message and annoy everyone.

Below is the function causing probs.

Ta
sree

############### ############### #
def fileplay(x):
global conf,bdir,bgpla y
lbuf.delete(lbu f.get_start_ite r(),lbuf.get_en d_iter())
dat=buf.get_tex t(buf.get_start _iter(),buf.get _end_iter())
if not len(dat)>0:
return

#see if the temporary dir to save temp gmc and midi exists
#if not create it
tempf=bdir+os.s ep+'temp'+os.se p
try:
if not os.path.exists( tempf):
os.mkdir(tempf)
except:
addlog(tracebac k.format_exc())
return
#save octavia in to a count+1 text file
if os.path.exists( tempf):
try:
fbase=tempf+get cnt()
fmidi=fbase+'.m idi'
f=open(fbase,'w ')
f.write(dat)
f.close()
except:
addlog(tracebac k.format_exc())
#run octavia
addlog("Compili ng to midi")
if conf.has_key('o ctavia'):
text2midi=conf['octavia']
else:
addlog("Config doesn't exist. Trying default octavia")
text2midi='octa via'
try:
res=os.popen3(t ext2midi+' '+fbase+' '+fmidi)
addlog(res[1].read()+res[2].read())
except:
addlog(tracebac k.format_exc())
return
# if midi exists, we succeded. play midi
if os.path.exists( fmidi):
addlog("Trying to play midi")
if conf.has_key('m idiplayer'):
midiplay=conf['midiplayer']
else:
addlog("Config doesn't exist. Trying default timidity")
midiplay='timid ity'

# start playing in a fork
pid=os.fork()
if pid:
pass
else:
os.popen3(midip lay+' '+fmidi)
sys.exit(0)
Jul 11 '06 #8
On 2006-07-10, sreekant <sk*****@lithiu m.comwrote:
Hi folks

What am I doing wrong in the following? I just want to run fluidsynth in
the background.
############### ############### ###########
class MyThread(thread ing.Thread):
def __init__(self, cmd, callback):
self.__cmd = cmd
self.__callback = callback
threading.Threa d.__init__(self )

def run(self):
os.system(self. __cmd)
self.__callback ('abcd')
return
cmd=midiplay+' '+fmidi
xc=MyThread(cmd ,addlog)
xc.start()
############### #######
midiplay is 'fluidsynth -ni /home/mysndfont.sf2 mymidi.mid'
addlog is a function which prints the log.

If I run it only with xc.start() it does not run the program as in
os.system. However if I put
xc.start()
xc.run()

then it starts and runs it in foreground with my pygtk ui non responsive.

What am I missing!
You may be missing nothing. If I recall correctly a similar problem was
once reported on the pygtk-list. Some investigation showed that some
programs couldn't be reliably run from a thread, using os.system.

--
Antoon Pardon
Jul 12 '06 #9
Oh dear. For the time being I will leave it with fork and leave it at that.

Ta
sree
You may be missing nothing. If I recall correctly a similar problem was
once reported on the pygtk-list. Some investigation showed that some
programs couldn't be reliably run from a thread, using os.system.
Jul 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2983
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
13
2717
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming Contest is scheduled on Sunday, 27 Feb 2005. This is the first Online Programming Contest in India to support Python !!!!. Other languages supported are C and C++.
2
2068
by: F. GEIGER | last post by:
In my wxPython-app a part of it gathers data, when a button is pressed, and stores it into a db. The GUI part should display the stuff being stored in the db. When both parts work on the same connection, I get "SQL statements in progress errors". Seems ok to me, you can't do that. So, next step: Both parts get a separate connection. Now I get "Database locked" errors. Hmm, yes, it's GUI stuff after all, being all in the same
0
1971
by: lmckaha | last post by:
Hi, Mysql version: 3.23.49 Solaris version: 2.7 gcc compiler version: 2.95.2 Python : 2.2.2 I'm evaluating the C and C++ API to decide which one to bye but I have many troubles.
77
5381
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
0
1488
by: Michal | last post by:
I have troubles with instaling .Net Framework 2.0 (Beta 2 - 2.0.50215). The main instalation went just fine, troubles begin with my attemt to run aspnet_regiss.exe -i. Asp.Net is instaled into IIS as it should be, sites are working just fine. The only problem is thet this instalation did not create the AspNet_WebAdmin folder in /inetpub/wwwroot, thus the WebAdmin is not instaled in IIS and I cannot administer my sites. I read in official...
11
2212
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a number of mail messages, reporting back to the UI thread through the use of a Queue object. When all messages that are expected have been received, each thread sends a final update to the UI and the method should exit, which should terminate the...
15
2075
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other synchronization objects instead and oh by the way you shouldn't be using them as they can cause deadlock and other major problems, screams bug or threading architecture issue and lack of willingness or priority to correct. In reality those API's are for...
0
1462
by: JohnIdol | last post by:
VC++6 to VC++2003 - linking troubles -------------------------------------------------------------------------------- Hi All, I successfully ported an application from VC++6 to VS2003. Solved compiling issues but now I am sticking with serious Linking troubles. I got a bunch of:
0
9538
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
9353
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
9975
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
9909
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
8794
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...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2765
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.