realtime design

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Oct 14 22:44:56 EDT 2002


Will Stuyvesant fed this fish to the penguins on Monday 14 October 2002 
07:29 am:

> 
> What I am going to look into is how to write a function that will
> start the target function in another *process* in Windows, and use a
> default value if the process does not finish in time.
> Any ideas?
>
        Not easy either -- Windows itself doesn't support many ways for a 
parent to wait on a sub-process ("Python Standard Library" indicates 
that os.system() waits, but my experience says it didn't). os.spawnv() 
has a WAIT mode, but then you are stuck just like in a thread...

        If you do NOWAIT, you could set up a timer... But again Windows 
doesn't have an easy way to kill the process (or I couldn't find it -- 
UNIX/Linux support os.kill(pid)). And you've no doubt seen just how 
long it takes from using the task manager (ctrl-alt-del) to pick a 
process to kill, until the "program is not responding..." pop-up.

        You'd also have the difficulty of how to get the result returned to 
you. Oh, besides having to write the "function" as a stand-alone main 
program.

        (Oh, I should clarify -- I do NOT know what is available in the win32 
extension package regarding process control; my book is on loan <G>)


        So... while I can visualize a scheme in which I can properly select 
which return is valid, I can not kill off the incomplete thread, and 
also have the restriction that a second call is unsafe before all of 
the first one has finished (well, I may have a work around for that too)

... a half hour later... (hope the formatting survives -- ignore the 
KNode "box")


,----[ /home/wulfraed/tt.py ]
| from time import sleep
| import threading
| import Queue
| 
| 
| def Worker(*args, **kwargs):
|     retq = kwargs["retq"]
|     func = kwargs["ToDo"]
|     del kwargs["retq"]
|     del kwargs["ToDo"]
|     retq.put(func(*args, **kwargs))
| 
| def rcall(ms, func, *ar, **kw):
|     q = Queue.Queue()
|     kw["retq"] = q
|     kw["ToDo"] = func
|     w = threading.Thread(target=Worker, args=ar, kwargs=kw)
|     w.start()
|     w.join(float(ms) / 1000.0)
|     if q.empty():
|         return "TimeOut"    #   leaves a dangling Queue and Worker 
Thread
        # had a line wrap on the comment, beware!
|     else:
|         return q.get()
| 
| 
| 
| #   dummy func
| 
| def dfunc(ms):
|     sleep(float(ms) / 1000.0)
|     return "dfunc is Defunct"
| 
| if __name__ == "__main__":
|     print rcall(10, dfunc, 5)
|     print rcall(10, dfunc, 50)
|     print rcall(50, dfunc, 50)
|     print rcall(50, dfunc, 49)
|     print rcall(50, dfunc, 51)
|     

`----

        
> 
> and-now-I-am-thinking-about-how-to-kill-a-Windows-process-ly y'rs
> 
> 
> '''
> Multithreading will rot your teeth.
>                 -- Michael Swaine
> '''

-- 
--  
 > ============================================================== <
 >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
 >      wulfraed at dm.net     |       Bestiaria Support Staff       <
 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <



More information about the Python-list mailing list