<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
<br>
Now if I could just turn that into a COM object (seriously, I'm trying to figure<br>
out how to call movingballs2.py, a Vpython script, from Visual FoxPro).<br>
<br></blockquote><div><br></div><div><br></div><div>OK, I turned it into a COM object, pretty mickey mouse, yet instructive (at least to me):</div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div class="gmail_quote">
<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div>VFP = Visual FoxPro and in the middle you'll see how I'm in the interactive shell of that language, and creating this Python object.</span></div>
<div class="gmail_quote"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div class="gmail_quote"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Even though control returns to the host language, the threads spawn and do their business, taking their time to fill a randomly named text file in some pre-specified directory.</span></div>
<div class="gmail_quote"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div class="gmail_quote"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">My thanks to Mark Hammond the one and only for swinging by on comp.lang.python -- more acknowledgment in my blog:</span></font></div>
<div class="gmail_quote"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div class="gmail_quote"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><a href="http://mybizmo.blogspot.com/2010/12/office-work.html">http://mybizmo.blogspot.com/2010/12/office-work.html</a></span></font></div>
<div class="gmail_quote"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse; "><br></span></font></div><div class="gmail_quote"><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">Here's the COM version (pycomsupport dependency not included for brevity)</span></font></div>
<div class="gmail_quote"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br>import threading<br>from random import choice, randint<br>import pycomsupport<br>
import os<br>import time<br><br>applause = ["Bravo!\n","Clap Clap\n","Yay!\n","whistle\n","shreek\n"]<br>laugh = ["chuckle\n", "guffaw\n", "hoot\n", "howl\n", "ROFL\n"]<br>
boo = ["Boo!\n", "Nada mas!\n", "hissss\n"]<br><br>class MPSS_Audience ( object ):<br><br> _public_methods_ = [ 'applause_track', 'laugh_track', 'boo_track' ]<br> _reg_progid_ = "LoadOpt.Feedback"<br>
<br> # Use pythoncom.CreateGuid()" to make a new clsid<br> _reg_clsid_ = '{BFE032DC-0F31-47CA-A714-7D6AADA41F5C}'<br><br> def __init__(self, ident = None):<br> self.logger = ident<br> fp = os.path.normpath("C:\Users\Kirby\Documents\Visual FoxPro<br>
Projects\mpss")<br> fn = pycomsupport.randomstring(8)+".txt"<br> self.output = os.path.join(fp, fn)<br><br> def _noise_threads(self, noise_type):<br> hnd = open(self.output, 'w')<br>
for i in range(10):<br> AudiencePerson(hnd, choice(noise_type), randint(0,5)).start()<br><br> def applause_track(self):<br> self._noise_threads(applause)<br><br> def laugh_track(self):<br> self._noise_threads(laugh)<br>
<br> def boo_track(self):<br> self._noise_threads(boo)<br><br><br>class AudiencePerson( threading.Thread ):<br><br> def __init__(self, output, an, interval):<br> self.output = output<br> self.audience_noise = an<br>
self.interval = interval<br> super(AudiencePerson, self).__init__()<br><br> def run(self):<br> for i in range(5):<br> self.output.write(self.getName() + ": " + self.audience_noise)<br>
time.sleep(self.interval)<br><br>def testme():<br> loObj = MPSS_Audience(1)<br> loObj.laugh_track()<br><br>if __name__ == "__main__":<br> print "Registering COM server..."<br> import win32com.server.register<br>
win32com.server.register.UseCommandLine(MPSS_Audience)<br> testme()<br><br><br>On the VFP side:<br><br>loCircus = CREATEOBJECT("Loadopt.Feedback", "some param")<br>loCircus.applause_track()<br><br>
control returns immediately, even though the text file<br>takes quite awhile to fill, thanks to the several second<br>delays between thread writes. The final file:<br><br>Thread-1: whistle<br>Thread-2: shreek<br>Thread-3: shreek<br>
Thread-4: whistle<br>Thread-5: Yay!<br>Thread-6: whistle<br>Thread-7: Yay!<br>Thread-7: Yay!<br>Thread-7: Yay!<br>Thread-7: Yay!<br>Thread-7: Yay!<br>Thread-8: Clap Clap<br>Thread-9: Clap Clap<br>Thread-9: Clap Clap<br>Thread-9: Clap Clap<br>
Thread-9: Clap Clap<br>Thread-9: Clap Clap<br>Thread-10: Clap Clap<br>Thread-2: shreek<br>Thread-8: Clap Clap<br>Thread-2: shreek<br>Thread-8: Clap Clap<br>Thread-2: shreek<br>Thread-4: whistle<br>Thread-5: Yay!<br>Thread-6: whistle<br>
Thread-8: Clap Clap<br>Thread-1: whistle<br>Thread-2: shreek<br>Thread-3: shreek<br>Thread-8: Clap Clap<br>Thread-10: Clap Clap<br>Thread-4: whistle<br>Thread-5: Yay!<br>Thread-6: whistle<br>Thread-1: whistle<br>Thread-3: shreek<br>
Thread-4: whistle<br>Thread-5: Yay!<br>Thread-6: whistle<br>Thread-10: Clap Clap<br>Thread-1: whistle<br>Thread-4: whistle<br>Thread-3: shreek<br>Thread-5: Yay!<br>Thread-6: whistle<br>Thread-10: Clap Clap<br>Thread-1: whistle<br>
Thread-3: shreek<br></span><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Thread-10: Clap Clap</span></div><div><br><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "></span></div>
<div>Kirby</div><div><br></div></div>