[Twisted-Python] Windows IOCP reactor Question
    Schneider, Michael 
    michael.l.schneider at ugsplm.com
       
    Sun Mar 28 08:26:16 MST 2004
    
    
  
Thanks for the work on IOCP,
I really appriecaite the efforts and will make use of
it.
I am an old UNIX hacker, and I am delivering a Windows
project with twisted.
I am about as far away from a windows programmer that you
can get.
I did have to get memory info from the Widows OS.
I was going to use C, but then I found ctypes and the
struct python C struct wrapper.
It was nice to have a python only solution.  
As I was looking through the code in iocpcoreobject.c.
I noticed that most (maybe all) of the functionality could
be accessed by the ctypes module and the Struct package.
Being a newbie to windows programming, I am not comfortable 
making the definitive statement that it should use this, and
elimintate the C code.  
Instead I would like to ask the question.  Does anyone see
a problem replacing the  C code in iocpcoreobject.c with
a python module that leverages ctype.
I attached a simple python script that gets the memory from
a windows box using ctypes.
It would be very nice if no c compiler would be required to 
install twisted IOCP reactor from CVS.
Thanks,
Mike
-------------------------------------------------------
#!/usr/bin/env python
# Simple WMI and ctypes example
# Michael Schnedier
import os
import time
import wmi
from ctypes import *
from ctypes.wintypes import *
import win32api
class MEMORYSTATUS(Structure):
    _fields_ = [
        ('dwLength', DWORD),
        ('dwMemoryLoad', DWORD),
        ('dwTotalPhys', DWORD),
        ('dwAvailPhys', DWORD),
        ('dwTotalPageFile', DWORD),
        ('dwAvailPageFile', DWORD),
        ('dwTotalVirtual', DWORD),
        ('dwAvailVirtual', DWORD),
    ]
class ComputeNode:
    def __init__(self):      
        self.memInfo = None
        self.lastUpdate = time.time()
        self.update()
   
    def genMemoryXmlStringRep(self):
        self.validate()
        xmlString = \
"""<?xml version="1.0"?>
<MemoryInfo platform="windows"     
TotalPhysicalMemory="%d" 
AvailablePhysicalMemory="%d" 
TotalPageFileSize="%d" 
AvailPageFileSize="%d" 
GetTotalVirtualMemory="%d" 
GetAvailableVirtualMemory="%d" 
/>
"""%( self.getTotalPhysicalMemory(),
       self.getAvailPhysicalMemory(),
       self.getTotalPageFileSize(),
       self.getAvailPageFile(),
       self.getTotalVirtualMemory(),
       self.getAvailVirtualMemory())
        return xmlString
   
    def getTotalPhysicalMemory(self):
        self.validate()
        return self.memInfo.dwTotalPhys/1024/1024
    def getAvailPhysicalMemory(self):
        self.validate()
        return self.memInfo.dwAvailPhys/1024/1024
    def getTotalPageFileSize(self):
        self.validate()
        return self.memInfo.dwTotalPageFile/1024/1024
    def getAvailPageFile(self):
        self.validate()
        return self.memInfo.dwAvailPageFile/1024/1024
    def getTotalVirtualMemory(self):
        self.validate()
        return self.memInfo.dwTotalVirtual/1024/1024
    def getAvailVirtualMemory(self):
        self.validate()
        return self.memInfo.dwAvailVirtual/1024/1024
    def validate(self, deltaTrigger=60 ):
        deltaTime = time.time() - self.lastUpdate
        
        if (self.lastUpdate < 0) or (deltaTime > deltaTrigger):      
            self.update()
      
    def update(self):
        self.lastUpdate = time.time()
        newStatus = MEMORYSTATUS()
        windll.kernel32.GlobalMemoryStatus(byref(newStatus))
        self.memInfo =  newStatus
         
machineInfo = ComputeNode()
print machineInfo.genMemoryXmlStringRep()
Since
the C code seems to be a wrapper to COM services, and
registration of COM service I am curious
 
----------------------------------------------------------------
Michael Schneider
Senior Software Engineering Consultant
UGS PLM Solutions - an EDS Company
 
"The Greatest Performance Improvement Is the transitioning from a non-working state to the working state"
> 
    
    
More information about the Twisted-Python
mailing list