is there better 32 clock() timing?
I have a need for a time.clock() with >0.16 second (16us) accuracy. The sleep() (on Python 2.3, Win32, at least) has a .001s limit. Are they lower/better on other's platforms? Test code, 2.4GHz P4 Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32 import time t0 = time.clock() t1 = time.clock() t2 = time.clock() t3 = time.clock() t4 = time.clock() t5 = time.clock() print (-t0+t5)/5. print t1-t0 print t2-t1 print t3-t2 print t4-t3 print t5-t4 >>> ave 0.000342754564927 0.000321028401686 0.00030348379596 0.000297101358228 0.000295895991258 I had also considered forking a thread that would spin a loop checking time.clock() and firing the TTL pulse after the appropriate interval, but the real, ultimate resolution of time.clock() appears to be ~.00035s. If I increase process priority to real-time, it is ~.00028s The alternative appears to be more C code... Ray BCI/Congitive Vision -- http://mail.python.org/mailman/listinfo/python-list
Re: [Py2exe-users] py2exe 0.6.2 released
First, Thanks again for the update. At 08:55 AM 9/7/2005, Thomas Heller wrote: > This part of the code is distributed under the MPL 1.1, so this > license is now pulled in by py2exe. As I read it, it seems that I need to include an Exibit A http://www.mozilla.org/MPL/MPL-1.1.html#exhibit-a filled out so that it includes the py2exe home, as well as Python, probably. It could be put in the Zip or Rar to be viewed on extraction. Does this sound correct? Ray Schumacher -- http://mail.python.org/mailman/listinfo/python-list
Python and real-time OS timing/task communication?
Has anyone used Python and a hard real-time OS/patch to schedule timed events? We have started in on Debian and RTAI, and may be using LXRT. (I've been reading http://people.mech.kuleuven.be/~psoetens/lxrt/portingtolxrt.html) I was envisioning that we really only need a separate RT-process in C using RDTSC or ACPI clocking that can get its control messages from non-RT Python every 50ms or so, to toggle pins on parport0. Any pointers, examples, etc for communicating from Python to an RT task? Thanks, Ray -- http://mail.python.org/mailman/listinfo/python-list
"parent" in a class __init__ def?
What is the feeling on using "parent" in a class definition that class methods can refer to, vs. some other organization ? Should all relevant objects/vars just be passed into the method as needed? It seems like including "parent" in the class def is just like a class variable, which most do not recommend. An example: class LXSerial: def __init__(self, parent, debug=False): ... def connect(self, port, baud=9600, ptimeout=10): if self.debug: self.connectedPort = StringIO.StringIO(':A#') else: if self.parent.model=='LX200GPS': ptimeout = 240 ... Ray -- http://mail.python.org/mailman/listinfo/python-list
from foo imprt * in package __init__ ?
What is the Pythonic-ness of using from foo imprt * in a package's __init__.py? I import my own (~8) module files in the package this way, and import standard modules with "import bar". Ray -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 33, Issue 159
Thanks Larry, My depth can really only get to ~3: package module module error_module an usually not that. It is shallow, with >hundred methods (mainly serial protocol defs for LX* telescopes), but it could grow I suppose. I mainly see its use as an import for other, large apps. Speed is not an issue here , just clutter, as you said. I still also have not seen a written consensus on the "proper" usage of class variables. I define module vars (some constants), which I think is reasonable, although these modules are the type with only one class: port = LXSerial.LXSerial(...) My rationale of putting the class in its own module is to minimize giant module files with lots of long classes; there is only a remote possibility that someone would want call a class without most of the others as well. Ray Ray Schumacher wrote: > > What is the feeling on using "parent" in a class definition that class > > methods can refer to, vs. some other organization ? > > Should all relevant objects/vars just be passed into the method as needed? > > It seems like including "parent" in the class def is just like a class > > variable, which most do not recommend. > Passing parent instance into a class is perfectly legal and is > used extensively in modules like wxPython GUI. It isn't really > anything like a class variable as the instance is normally > passed not the class itself. Each instance can have different > attributes. So if you have many parents with many children this > can be an effective way to structure them. > > I think it depends on how deeply nested things get and how many > parameters need to be passed. I've used it when I want to > nest my objects more than 2 deep and I must pass around lots of > attributes. I find it is easier to just look "upwards" into the > parent to get the attribute than to clutter up my argument list > passing arguments deeper and deeper into the class hierarchy. > It can simplify the argument lists quite a bit. Maybe others can > comment with their thoughts as well. -- http://mail.python.org/mailman/listinfo/python-list
"parent" in a class __init__ def?
Thanks Larry, My depth really only gets to ~3: package module module error_module and usually not that. It is shallow, with >hundred methods (mainly serial protocol defs for LX* telescopes), but it could grow modules, I suppose. I mainly see its use as an import for other, large apps. Speed is not an issue here, just clutter, as you'd said. I also have not seen a written consensus on the "proper" usage of class variables, if any. I define module vars (some constants), which I think is reasonable, although these modules are the type with only one class: port = LXSerial.LXSerial(...) My rationale of putting one class in its own module is to minimize giant module files with lots of long classes; I see only a remote possibility that someone would want call a class without most of the others as well. Ray Ray Schumacher wrote: > > What is the feeling on using "parent" in a class definition that class > > methods can refer to, vs. some other organization ? > > Should all relevant objects/vars just be passed into the method as needed? > > It seems like including "parent" in the class def is just like a class > > variable, which most do not recommend. > Passing parent instance into a class is perfectly legal and is > used extensively in modules like wxPython GUI. It isn't really > anything like a class variable as the instance is normally > passed not the class itself. Each instance can have different > attributes. So if you have many parents with many children this > can be an effective way to structure them. > > I think it depends on how deeply nested things get and how many > parameters need to be passed. I've used it when I want to > nest my objects more than 2 deep and I must pass around lots of > attributes. I find it is easier to just look "upwards" into the > parent to get the attribute than to clutter up my argument list > passing arguments deeper and deeper into the class hierarchy. > It can simplify the argument lists quite a bit. Maybe others can > comment with their thoughts as well. -- http://mail.python.org/mailman/listinfo/python-list
how to serve image files without disk use?
I'm trying to make a small camera server using VideoCapture.py and socket. I needed to construct a complete image file with headers etc for a browser to recognize it, but I couldn't find a combination of StringIO and wx image methods to avoid disk saves, without PIL. If I save a temp.jpg file to disk I can serve the image easily: ... self.cam = VideoCapture.Device(devnum=0, showVideoWindow=0) buff, width, height = self.cam.dev.getbuffer() im = wx.EmptyImage(width, height) im.SetData(buff) im.Mirror().SaveFile('temp.jpg', wx.BITMAP_TYPE_JPEG) ... s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() while 1: data = conn.recv(1024) if data[:3] == 'GET': conn.send("HTTP/1.0 200 OK"+"\015\012") conn.send("Server: RJS_video/0.0.1"+"\015\012") conn.send("Content-type: image/bmp"+"\015\012") conn.send("Content-Length: "+str(352*288*3+256)+"\015\012") conn.send("\015\012") fh = file('temp.jpg', 'rb') conn.send(fh.read()) fh.close() else: break But, how can I avoid disk writes? wx's *.SaveFile() needs a string file name (no objects). I'm going to investigate PIL's im.save(), as it appears to allow file-objects. Ray -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 39, Issue 465
At 10:50 AM 12/29/2006, you wrote: In addition to what Chris said, is there a reason why you're reinventing the wheel instead of using available components? Hi Carsten, The eventual goal here is towards a streaming two-way server, which wouldn't use the http, but something more like RTP/RTCP/H.323/ which I'm learning. It's also a general socketĀ learning exercise for me, as I haven't used them very much. I was also using PyDShowCam before, and have switched to VideoCapture since I'll need to recompile PyDShowCam for py2.4. I will look into the wx Chris suggested this weekend; more stuff I haven't used... Thanks, Ray For anyone interested, I attached a quick wx I made last night that monitors a USB webcam, will create "dark" frames (a la astronomy), and serve up images to web browsers (thus the question). The browser/monitor would actually work nicely with a web page that has a _javascript_ function to reload the image x times/second. I'll try replacing the PIL with the wx function(s) and benchmark if they work. #Boa:Frame:Frame1 import gc from time import clock from numpy import fromstring, add, subtract, divide, where, zeros import socket import wx #import PyDShowCam import VideoCapture from StringIO import StringIO from PIL import Image def create(parent): return Frame1(parent) [wxID_FRAME1, wxID_FRAME1BUTTONCONNECT, wxID_FRAME1BUTTONPROPERTIES, wxID_FRAME1BUTTONSERVER, wxID_FRAME1BUTTONSETDARK, wxID_FRAME1BUTTONSTOP, wxID_FRAME1CHECKBOXDIFF, wxID_FRAME1PANEL1, wxID_FRAME1STATICBITMAPVIDEO, wxID_FRAME1STATUSBAR1, wxID_FRAME1TEXTCTRLADDRESS, wxID_FRAME1TOOLBAR1, wxID_FRAME1WINDOWVIDEO, ] = [wx.NewId() for _init_ctrls in range(13)] class Frame1(wx.Frame): def _init_coll_toolBar1_Tools(self, parent): # generated method, don't edit parent.AddControl(control=self.textCtrlAddress) parent.AddControl(control=self.buttonConnect) parent.AddControl(control=self.buttonStop) parent.AddControl(control=self.buttonProperties) parent.AddControl(control=self.checkBoxDiff) parent.AddControl(control=self.buttonSetDark) parent.AddControl(control=self.buttonServer) parent.Realize() def _init_coll_statusBar1_Fields(self, parent): # generated method, don't edit parent.SetFieldsCount(3) parent.SetStatusText(number=0, text='"frame:0,\t0fps"') parent.SetStatusText(number=1, text='') parent.SetStatusText(number=2, text='') parent.SetStatusWidths([200, -1, 100]) def _init_ctrls(self, prnt): # generated method, don't edit wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(278, 392), size=wx.Size(746, 377), style=wx.DEFAULT_FRAME_STYLE, title='Confer') self.SetClientSize(wx.Size(738, 350)) self.statusBar1 = wx.StatusBar(id=wxID_FRAME1STATUSBAR1, name='statusBar1', parent=self, style=0) self._init_coll_statusBar1_Fields(self.statusBar1) self.SetStatusBar(self.statusBar1) self.toolBar1 = wx.ToolBar(id=wxID_FRAME1TOOLBAR1, name='toolBar1', parent=self, pos=wx.Point(0, 0), size=wx.Size(767, 27), style=wx.TB_HORIZONTAL) self.SetToolBar(self.toolBar1) self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self, pos=wx.Point(0, 27), size=wx.Size(738, 303), style=wx.TAB_TRAVERSAL) self.panel1.SetBackgroundColour(wx.Colour(255, 255, 255)) self.windowVideo = wx.Window(id=wxID_FRAME1WINDOWVIDEO, name='windowVideo', parent=self.panel1, pos=wx.Point(8, 8), size=wx.Size(352, 288), style=0) self.windowVideo.SetBackgroundColour(wx.Colour(0, 0, 0)) self.textCtrlAddress = wx.TextCtrl(id=wxID_FRAME1TEXTCTRLADDRESS, name='textCtrlAddress', parent=self.toolBar1, pos=wx.Point(0, 0), size=wx.Size(100, 21), style=0, value='192.168.0.1') self.textCtrlAddress.SetToolTipString('Enter the remote address here') self.buttonConnect = wx.Button(id=wxID_FRAME1BUTTONCONNECT, label='Connect', name='buttonConnect', parent=self.toolBar1, pos=wx.Point(100, 1), size=wx.Size(60, 19), style=0) self.buttonConnect.Bind(wx.EVT_BUTTON, self.OnButtonConnectButton, id=wxID_FRAME1BUTTONCONNECT) self.buttonStop = wx.Button(id=wxID_FRAME1BUTTONSTOP, label='Stop', name='buttonStop', parent=self.toolBar1, pos=wx.Point(160, 1), size=wx.Size(49, 19), style=0) self.buttonStop.Bind(wx.EVT_BUTTON, self.OnButtonStopButton, id=wxID_FRAME1BUTTONSTOP) self.buttonProperties = wx.Button(id=wxID_FRAME1BUTTONPROPERTIES, label='Cam Properties', name='buttonProperties', parent=self.toolBar1, pos=wx.Point(209, 1), size=wx.Size(88, 19), style=0) self.buttonProperties.Bin
multi-threaded webcam with SimpleAsyncHTTPServer.py
The class seems to work pretty well - very basic and fast, it just serves images from the specified port and camera device (USB cam). I added an ImageServer class and some code to __main__ class ImageServer(RequestHandler): def __init__(self, conn, addr, server): asynchat.async_chat.__init__(self,conn) self.client_address = addr self.connection = conn self.server = server self.set_terminator ('\r\n\r\n') self.incoming = deque() self.outgoing = deque() self.rfile = None self.wfile = writewrapper(self.outgoing, -self.use_buffer or self.blocksize) self.found_terminator = self.handle_request_line self.request_version = "HTTP/1.1" self.code = None def send_head(self): buff, width, height = cam.dev.getbuffer() imPIL = (Image.frombuffer("RGB", (width, height), buff, "raw", "BGR", 0, -1) ) imPIL.save(PILFile, "JPEG") self.send_response(200) self.send_header("Content-type", "image/jpeg") self.send_header("Content-Length: ", str(PILFile.len)) self.end_headers() self.wfile.write(PILFile.getvalue()) return in __main__ ... parser.add_option('-3', dest='server', help='Run the server for only cam images', action='store_const', const=3) ... if options.server==3: from StringIO import StringIO from PIL import Image if sys.platform == 'win32': import VideoCapture try: del(cam) gc.collect() print "deleted old cam instance" except: pass cam = VideoCapture.Device(devnum=options.devNum, showVideoWindow=0) print cam buff, width, height = cam.dev.getbuffer() PILFile = StringIO() else: pass else: if options.root is None: parser.error("Need root path to start server") if not os.path.isdir(options.root): parser.error("Root path does not exist") os.chdir(options.root) I run it with: >python SimpleAsyncHTTPServer.py -p -3 I'll be trying implementing some streaming next. Question, though: how can I unblock asyncore.loop(), or at least be able to interrupt it? To kill this server I need to hit CNTRL-C and then attempt to GET an image from Firefox, Python then throws KetboardInterrupt. Note that I haven't tried it on *NIX so the sys.platform bit needs more implementing as well. Other suggestions? Ray""" Get an updated version of this server from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440665 Original pseudo-async* version by Pierre Quentel, available from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259148 * It would only be async while reading requests, it would switch to a synchronous write to a socket when writing a response to a socket. Ick. Features this version offers: 1. Uses asynchronous reading and writing to/from sockets all the time. 2. Performs sectioned reads from on-disk files, allowing for serving files of unlimited size. 3. More informative logging. 4. A quick wrapper for logging to a file and stdout/stderr. 5. Trailing-slash redirects on directories. 6. Optional built-in favicon.ico (to reduce the number of spurious log entries). 7. Subclass which does not allow any directory lists or redirecting to /index.htm[l] . 8. Subclass which does not allow any directory lists, but does automatic redirection to /index.htm[l] . 9. Doesn't try to open reserved names on Windows. 10. Has a tuning mechanism to change buffer performance depending on small or large files. For most people, one can run this from the command line and get a reasonably functioning web server with minor issue. I benchmarked this version in my personal projects folder, which contains around a gig of files, sizes ranging from a few kilobytes, to 100 megabytes. I then performed two concurrent "wget -m http://localhost/"; calls (from different paths) on Windows 2k (machine is a P4 2.8ghz with hyperthreading, 1.5 gigs memory, reading from an 80 gig SATA drive, writing to a 120 gig PATA drive). On small files, it was able to serve up 15-30 files/second. On larger (10+ meg) files, it was able to serve up at 15+ megs/second (so says adding the speed reported by wget). The server never broke 7 megs of resident memory, and tended to hang below 10% processor utilization. There exists a live host running this web server: nada.ics.uci.edu """ import asynchat, asyncore, socket, SimpleHTTPServer import sys, cgi, cStringIO, os, traceback, zlib, optparse __version__ = ".5" try: #Py
Re: multi-threaded webcam with SimpleAsyncHTTPServer.py
>>Question, though: how can I unblock asyncore.loop(), or at least be >>able to interrupt it? >Why do you want to do that? I was then thinking along the lines of a Netmeeting/visual chat program, rather than a daemon-type server, where one might want to terminate more quickly. Searching further, I see that a common idiom is like: while some_condition: try: asyncore.loop(timeout=1, count = 1) except KeyboardInterrupt: break or, spawn asyncore.loop() threads. Zope and Medusa seem to use a variety of methods. A regular web-cam program serving up 2 or 3 cams on one machine will require more thought on my part, of course. I only run one cam, but a friend runs 3 and is pissed at the X10 software he paid for. Thanks, Ray -- http://mail.python.org/mailman/listinfo/python-list
Re: multi-threaded webcam with SimpleAsyncHTTPServer.py (updated)
Better asyncore.loop use. Also fixes a late bug in my first post of code: PILFile.seek(0) needed since PIL.save does not reset the pointer. class ImageServer(RequestHandler): def __init__(self, conn, addr, server): asynchat.async_chat.__init__(self,conn) self.client_address = addr self.connection = conn self.server = server self.set_terminator ('\r\n\r\n') self.incoming = deque() self.outgoing = deque() self.rfile = None self.wfile = writewrapper(self.outgoing, -self.use_buffer or self.blocksize) self.found_terminator = self.handle_request_line self.request_version = "HTTP/1.1" self.code = None def send_head(self): buff, width, height = cam.dev.getbuffer() imPIL = (Image.frombuffer("RGB", (width, height), buff, "raw", "BGR", 0, -1) ) PILFile.seek(0) imPIL.save(PILFile, "JPEG") self.send_response(200) self.send_header("Content-type", "image/jpeg") self.send_header("Content-Length: ", str(PILFile.len)) self.end_headers() self.wfile.write(PILFile.getvalue()) return ... parser.add_option('-3', dest='server', help='Run the server for cam images', action='store_const', const=3) options, args = parser.parse_args() if options.server==3: from StringIO import StringIO from PIL import Image if sys.platform == 'win32': import VideoCapture try: del(cam) gc.collect() print "deleted old cam instance" except: pass cam = VideoCapture.Device(devnum=options.devNum, showVideoWindow=0) print cam buff, width, height = cam.dev.getbuffer() PILFile = StringIO() else: pass # try http://laurent.pointal.org/python/projets/pyvideograb/index.pih # or fg #import fg #cam = fg.Grabber() #cam.set_source(options.devNum) # or video4linux.video_capture / v4lctl else: if options.root is None: parser.error("Need root path to start server") if not os.path.isdir(options.root): parser.error("Root path does not exist") os.chdir(options.root) req_handler = which[options.server] s=Server('',options.port,req_handler) print req_handler.__name__, "running on port", options.port, "with root path", options.root while True: try: asyncore.loop(timeout=1, count=1) except KeyboardInter -- http://mail.python.org/mailman/listinfo/python-list
is there a posh Win32 binary? or, compiling with MS 2005 Express...
Is there a posh Win32 binary? Or better, has anyone successfully compiled modules for Python 2.4 with the newest "free" tools? Do I need to move to 2.5? I could not get it to compile with my (apparently incomplete) MS C++ 7.1 install (which did, however, work for weave/blitz, before). The 7.1 compiler with the 1.1SDK is no longer available, so I can't repair the install. I went through the motions of compiling yesterday with the new MS Express Toolkit on another machine and failed. Distutils keeps saying that the SDK for 7.1 is not installed. Ray -- http://mail.python.org/mailman/listinfo/python-list
threads - was:Is there a way to protect a piece of critical code?
"Hendrik van Rooyen" wrote: > Similarly discrete background thread jobs can be used > in a functional style this way: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491280 > ( an alternative for the laborious OO-centric threading. With the various options available, many of which I haven't used (CallQueue for example), I am wondering what the "best" methodology to use is for a 2-task example on a dual core processor for my next project: -One task is an ADC data collection/device driver whose sole job in life is to fill a numpy "circular buffer" with data from an external ADC device at high speed and expose a pointer - programmatically complex but functionally distinct from the analysis task. It is I/O bound and spends time waiting on the ADC. -The main task is analysis of that data in near-real time with FFT, correlations etc, and computationally bound. It needs to have read access to the array and a pointer, and kill the ADC task when desired. Thoughts/opinions are humbly requested, Ray -- http://mail.python.org/mailman/listinfo/python-list