Ok I see this will definitely not work as we are talking about threads and not processes so os.kill is of no use to me.  I guess there is no way to track these new threads and stop/pause/restart them based on some other decision logic?  Would have been nice if there were though...  -joe
<br><br><div><span class="gmail_quote">2007/12/16, Josef Novak &lt;<a href="mailto:josef.robert.novak@gmail.com">josef.robert.novak@gmail.com</a>&gt;:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Hmm.&nbsp;&nbsp;This seems like a pretty nice solution, except that it seems it will be blocking incoming calls during processing.&nbsp;&nbsp;What I really need to do is pause the deferToThread objects based on their pid.<br><br>How can I obtain the pid of a new deferToThread object?&nbsp;&nbsp;If I can obtain it, then I can just keep a persistent dictionary containing all the running compute-intensive processes, and unregister each one in a callback after it finishes up.&nbsp;&nbsp;Then, every time a new call comes in, I check my persistent pid dictionary/list, 
<br>for pid in pid_list:<br>&nbsp;&nbsp;os.kill(pid, signal.SIGSTOP)<br><br>and every time a call finishes, I check the number of current calls, and then if we&#39;re back to zero, we resume the compute intensive processes again:<br>

if parentObj.live_calls == 0::<br>&nbsp;&nbsp;for pid in parentObj.pid_list:<br>&nbsp;&nbsp;&nbsp;&nbsp;os.kill(pid, signal.SIGCONT)<br><br>so, my new question is... <br><span style="font-weight: bold;">How can I obtain the pid of a new deferToThread object?&nbsp;
</span>&nbsp;<br><br>I&#39;m pretty sure that, while not very elegant, this should solve my problem - but please let me know if this sounds really, cockamamey!<div><span class="e" id="q_116e1071bc43ff3c_1"><br><br>-Joe<br><br>
&gt; ------------------------------<br>&gt; <br>
&gt; Message: 2<br>&gt; Date: Sat, 15 Dec 2007 07:50:02 -0800<br>&gt; From: Ed Suominen &lt;<a href="mailto:general@eepatents.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">general@eepatents.com
</a>&gt;<br>&gt; Subject: Re: [Twisted-Python] finer control of deferToThread ?
<br>&gt; To: Twisted general discussion &lt;<a href="mailto:twisted-python@twistedmatrix.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">twisted-python@twistedmatrix.com</a>&gt;<br>&gt; Message-ID: &lt;
<a href="mailto:4763F7AA.2090800@eepatents.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">4763F7AA.2090800@eepatents.com
</a>&gt;<br>&gt; Content-Type: text/plain; charset=ISO-8859-1<br>&gt; <br>&gt; You can do priority queuing to one or more threads using AsynQueue,<br>&gt; <a href="http://foss.eepatents.com/AsynQueue" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://foss.eepatents.com/AsynQueue
</a> or python-asynqueue in Debian. Use<br>&gt; multiple threads and queue up the compute-intensive calls with a low<br>&gt; priority and the other stuff with higher priorities.<br>&gt; <br>&gt; However, you will need to chop up your compute-intensive stuff into
<br>&gt; smaller pieces for this to be helpful. (That&#39;s good asynchronous<br>&gt; processing practice, generally.) The priority queuing is only effective<br>&gt; at deciding which calls to dispatch next, and each thread call is on its
<br>&gt; own once it is dispatched from the queue. Each call uses an entire<br>&gt; thread for its entire duration, and will keep the queue from dispatching<br>&gt; anything else to that thread while it&#39;s squatting on it, no matter how
<br>&gt; low-priority it is.<br>&gt; <br>&gt; Best regards, Ed<br>&gt; <br>&gt; Josef Novak wrote:<br>&gt; &gt; Hi,<br>&gt; &gt;&nbsp;&nbsp; I am writing a twisted application using StarPY fastagi API for<br>&gt; &gt; Asterisk.&nbsp;&nbsp;My application involves answering user calls, and then
<br>&gt; &gt; based on their responses to an IVR dialogue, running some some<br>&gt; &gt; compute-intensive applications on the same server, after they hang up.<br>&gt; &gt;&nbsp;&nbsp; At the moment I am running the compute-intensive application (3rd
<br>&gt; &gt; party code) in a deferred.deferToThread.&nbsp;&nbsp;The application works fine,<br>&gt; &gt; however if the number of callers goes above 2-3, and one of the<br>&gt; &gt; compute-intensive applications from a previous call has not finished
<br>&gt; &gt; up, the audio for the call gets very jumpy because of CPU usage.<br>&gt; &gt;&nbsp;&nbsp; This compute-intensive process needs to be run immediately after<br>&gt; &gt; hangup, and I&#39;d prefer to take care of everything on the same machine
<br>&gt; &gt; (rather than send the compute-intensive application request somewhere<br>&gt; &gt; else).<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp; What I&#39;d like to do is pause the thread with the<br>&gt; &gt; deferred.deferToThread
 process any time a new&nbsp;&nbsp;call comes in (this
<br>&gt; &gt; sort of violates what I&#39;m saying above but the number of calls going<br>&gt; &gt; to a particular ASterisk trunk is limited so in most cases this would<br>&gt; &gt; never result in more than a couple-seconds delay, which is
<br>&gt; &gt; acceptable).<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp; Is there any way to control these deferred.deferToThread objects in<br>&gt; &gt; a more fine-grained manner?&nbsp;&nbsp;Say from a reactor factory?&nbsp;&nbsp;Can I<br>&gt; &gt; register them separately somehow, and then pause these
<br>&gt; &gt; compute-intensive applications temporarily every time a new call comes<br>&gt; &gt; in?&nbsp;&nbsp;I&#39;m imagining something as simple as as ctrl+z and $ fg linux<br>&gt; &gt; terminal commands... but I appreciate that it is probably not so
<br>&gt; &gt; straightforward.<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp; -Joe<br>&gt; &gt;<br>&gt; &gt; _______________________________________________<br>&gt; &gt; Twisted-Python mailing list<br>&gt; &gt; <a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

Twisted-Python@twistedmatrix.com</a><br>&gt; &gt; <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
</a><br>&gt; &gt;<br>&gt; &gt;<br>
&gt; &gt;<br>&gt; <br>&gt; <br>&gt; <br>&gt; ------------------------------<br>&gt; <br>&gt; _______________________________________________<br>&gt; Twisted-Python mailing list<br>&gt; <a href="mailto:Twisted-Python@twistedmatrix.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">

Twisted-Python@twistedmatrix.com</a><br>&gt; <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
</a><br>&gt; <br>&gt; <br>&gt; End of Twisted-Python Digest, Vol 45, Issue 18
<br>&gt; **********************************************<br>&gt; <br>
</span></div></blockquote></div><br>