def _output_dbdebug_string(s): s = str(s) mm = mmapfile.mmapfile(None, 'DBNSIS_BUFFER', 4096, 0, 4096) buffer_event = win32event.OpenEvent(win32con.SYNCHRONIZE, 0, 'DBNSIS_BUFFER_READY') data_event = win32event.OpenEvent(win32con.EVENT_MODIFY_STATE, 0, 'DBNSIS_DATA_READY') win32event.WaitForSingleObject(buffer_event, 10000) mm.seek(0) frag = struct.pack('L', win32api.GetCurrentProcessId()) + s.rstrip()[:2000].encode('utf16')[2:] + '\x00\x00' mm.write(frag) mm.flush() win32event.SetEvent(data_event)
def socket_send_msg(socket_fn, msg): """ 非bsd系统的进程间通信,发送消息,使用windows全局共享内存实现,函数名称保持与bsd的接口名称一致 :param socket_fn: : 共享内存名称 :param msg: 字符串类型需要传递的数据,不需要encode,内部进行encode """ global_fn = 'Global\\{}'.format(socket_fn) event = w32e.OpenEvent(w32e.EVENT_ALL_ACCESS, 0, global_fn) event_mmap = mmf.mmapfile(None, socket_fn, 1024) w32e.SetEvent(event) event_mmap.write(msg) event_mmap.close() win_api.CloseHandle(event)
def __init__(self): self._mapname = '%s%08x' % (_AGENT_NAME, win32api.GetCurrentThreadId()) try: self._mapfile = mmapfile.mmapfile(None, self._mapname, _AGENT_MAX_MSGLEN, 0, 0) except mmapfile.error as exc: raise OSError(errno.EIO, str(exc)) from None self._cds = _CopyDataStruct(_AGENT_COPYDATA_ID, len(self._mapname) + 1, self._mapname.encode()) self._writing = False
def socket_bind_recv(socket_fn, cmd_handler): """ 非bsd系统的进程间通信,接受消息,处理消息,使用windows全局共享内存实现, 函数名称保持与bsd的接口名称一致 :param socket_fn: 共享内存文件名称 :param cmd_handler: cmd处理函数,callable类型 """ if not callable(cmd_handler): print('socket_bind_recv cmd_handler must callable!') while True: global_fn = 'Global\\{}'.format(socket_fn) event = w32e.CreateEvent(None, 0, 0, global_fn) event_mmap = mmf.mmapfile(None, socket_fn, 1024) w32e.WaitForSingleObject(event, -1) socket_cmd = event_mmap.read(1024).decode() # 把接收到的socket传递给外部对应的处理函数 cmd_handler(socket_cmd) event_mmap.close() win_api.CloseHandle(event)
def load(fname, lines_to_ignore=4, type='i'): """ Load in huge, flat, 2D text files. Can handle differing line-lengths AND can strip #/% on UNIX (or with a better NT grep). Requires wc, grep, and mmapfile.lib/.pyd. Type can be 'i', 'f' or 'd', for ints, floats or doubles, respectively. Lines_to_ignore determines how many lines at the start of the file to ignore (required for non-working grep). Usage: load(fname,lines_to_ignore=4,type='i') Returns: numpy array of specified type """ start = time.time() ## START TIMER if type == 'i': intype = int elif type in ['f', 'd']: intype = float else: raise ValueError("type can be 'i', 'f' or 'd' in load()") ## STRIP OUT % AND # LINES tmpname = tempfile.NamedTemporaryFile(delete=False).name if sys.platform == 'win32': # NT VERSION OF GREP DOESN'T DO THE STRIPPING ... SIGH cmd = "grep.exe -v \'%\' " + fname + " > " + tmpname print(cmd) os.system(cmd) else: # UNIX SIDE SHOULD WORK cmd = "cat " + fname + " | grep -v \'%\' |grep -v \'#\' > " + tmpname print(cmd) os.system(cmd) ## GET NUMBER OF ROWS, COLUMNS AND LINE-LENGTH, USING WC wc = string.split(os.popen("wc " + tmpname).read()) numlines = int(wc[0]) - lines_to_ignore tfp = open(tmpname) if lines_to_ignore != 0: for i in range(lines_to_ignore): junk = tfp.readline() numcols = len(string.split(tfp.readline())) #int(float(wc[1])/numlines) tfp.close() ## PREPARE INPUT SPACE a = np.zeros((numlines * numcols), type) block = 65536 # chunk to read, in bytes data = mmapfile.mmapfile(tmpname, '', 0) if lines_to_ignore != 0 and sys.platform == 'win32': for i in range(lines_to_ignore): junk = data.readline() i = 0 d = ' ' carryover = '' while len(d) != 0: d = carryover + data.read(block) cutindex = string.rfind(d, '\n') carryover = d[cutindex + 1:] d = d[:cutindex + 1] d = list(map(intype, string.split(d))) a[i:i + len(d)] = d i = i + len(d) end = time.time() print("%d sec" % round(end - start, 2)) data.close() os.remove(tmpname) return np.reshape(a, [numlines, numcols])
import win32api, mmapfile import winerror import tempfile, os from pywin32_testutil import str2bytes system_info=win32api.GetSystemInfo() page_size=system_info[1] alloc_size=system_info[7] fname=tempfile.mktemp() mapping_name=os.path.split(fname)[1] fsize=8*page_size print(fname, fsize, mapping_name) m1=mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize) m1.seek(100) m1.write_byte(str2bytes('?')) m1.seek(-1,1) assert m1.read_byte()==str2bytes('?') ## A reopened named mapping should have exact same size as original mapping m2=mmapfile.mmapfile(Name=mapping_name, File=None, MaximumSize=fsize*2) assert m2.size()==m1.size() m1.seek(0,0) m1.write(fsize*str2bytes('s')) assert m2.read(fsize)==fsize*str2bytes('s') move_src=100 move_dest=500 move_size=150
def load(fname, lines_to_ignore=4, type="i"): """ Load in huge, flat, 2D text files. Can handle differing line-lengths AND can strip #/% on UNIX (or with a better NT grep). Requires wc, grep, and mmapfile.lib/.pyd. Type can be 'i', 'f' or 'd', for ints, floats or doubles, respectively. Lines_to_ignore determines how many lines at the start of the file to ignore (required for non-working grep). Usage: load(fname,lines_to_ignore=4,type='i') Returns: numpy array of specified type """ start = time.time() ## START TIMER if type == "i": intype = int elif type in ["f", "d"]: intype = float else: raise ValueError, "type can be 'i', 'f' or 'd' in load()" ## STRIP OUT % AND # LINES tmpname = tempfile.mktemp() if sys.platform == "win32": # NT VERSION OF GREP DOESN'T DO THE STRIPPING ... SIGH cmd = "grep.exe -v '%' " + fname + " > " + tmpname print cmd os.system(cmd) else: # UNIX SIDE SHOULD WORK cmd = "cat " + fname + " | grep -v '%' |grep -v '#' > " + tmpname print cmd os.system(cmd) ## GET NUMBER OF ROWS, COLUMNS AND LINE-LENGTH, USING WC wc = string.split(os.popen("wc " + tmpname).read()) numlines = int(wc[0]) - lines_to_ignore tfp = open(tmpname) if lines_to_ignore <> 0: for i in range(lines_to_ignore): junk = tfp.readline() numcols = len(string.split(tfp.readline())) # int(float(wc[1])/numlines) tfp.close() ## PREPARE INPUT SPACE a = N.zeros((numlines * numcols), type) block = 65536 # chunk to read, in bytes data = mmapfile.mmapfile(tmpname, "", 0) if lines_to_ignore <> 0 and sys.platform == "win32": for i in range(lines_to_ignore): junk = data.readline() i = 0 d = " " carryover = "" while len(d) <> 0: d = carryover + data.read(block) cutindex = string.rfind(d, "\n") carryover = d[cutindex + 1 :] d = d[: cutindex + 1] d = map(intype, string.split(d)) a[i : i + len(d)] = d i = i + len(d) end = time.time() print "%d sec" % round(end - start, 2) data.close() os.remove(tmpname) return N.reshape(a, [numlines, numcols])
import win32api, mmapfile import winerror import tempfile, os from pywin32_testutil import str2bytes system_info = win32api.GetSystemInfo() page_size = system_info[1] alloc_size = system_info[7] fname = tempfile.mktemp() mapping_name = os.path.split(fname)[1] fsize = 8 * page_size print(fname, fsize, mapping_name) m1 = mmapfile.mmapfile(File=fname, Name=mapping_name, MaximumSize=fsize) m1.seek(100) m1.write_byte(str2bytes("?")) m1.seek(-1, 1) assert m1.read_byte() == str2bytes("?") ## A reopened named mapping should have exact same size as original mapping m2 = mmapfile.mmapfile(Name=mapping_name, File=None, MaximumSize=fsize * 2) assert m2.size() == m1.size() m1.seek(0, 0) m1.write(fsize * str2bytes("s")) assert m2.read(fsize) == fsize * str2bytes("s") move_src = 100 move_dest = 500 move_size = 150
def _getImage(self): # {'type': numpy.ndarray} # 0 uses internal flash signal # 1 uses internal exposure signal (PVCam and PXL only) # shutter_mode = 1 offset = dict(self.getOffset()) dimension = self.getDimension() binning = self.getBinning() camerasize = self.getCameraSize() mirror = self.getMirror() if mirror == 'vertical': offset['x'] = camerasize['x']/binning['x'] - offset['x'] - dimension['x'] elif mirror == 'horizontal': offset['y'] = camerasize['y']/binning['y'] - offset['y'] - dimension['y'] elif mirror == 'both': offset['x'] = camerasize['x']/binning['x'] - offset['x'] - dimension['x'] offset['x'] = camerasize['x']/binning['x'] - offset['x'] - dimension['x'] # rotation untested rotation = self.getRotation() if rotation == 90: offset['x'] = camerasize['y']/binning['y'] - offset['y'] - dimension['y'] offset['y'] = offset['x'] elif rotation == 180: offset['x'] = camerasize['x']/binning['x'] - offset['x'] - dimension['x'] offset['y'] = camerasize['y']/binning['y'] - offset['y'] - dimension['y'] elif rotation == 270: offset['x'] = offset['y'] offset['y'] = camerasize['x']/binning['x'] - offset['x'] - dimension['x'] cameracontrol.lock() cameracontrol.setCamera(self) hr = cameracontrol.camera.Format( offset['x']*binning['x'], offset['y']*binning['y'], dimension['x'], dimension['y'], binning['x'], binning['y']) exposuretype = self.getExposureType() t0 = time.time() if exposuretype == 'normal': hr = cameracontrol.camera.AcquireImage(self.getExposureTime(), 0) elif exposuretype == 'dark': hr = cameracontrol.camera.AcquireDark(self.getExposureTime(), 0) elif exposuretype == 'bias': hr = cameracontrol.camera.AcquireBias(0) elif exposuretype == 'readout': hr = cameracontrol.camera.AcquireReadout(0) else: cameracontrol.unlock() raise ValueError('invalid exposure type for image acquisition') t1 = time.time() self.exposure_timestamp = (t1 + t0) / 2.0 cameracontrol.unlock() imagesize = self.bytesperpixel*dimension['x']*dimension['y'] map = mmapfile.mmapfile('', self.mmname, imagesize) na = numpy.fromstring(map.read(imagesize), self.imagetype) map.close() na.shape = (dimension['y'], dimension['x']) return na
def get_current_track_info(self): """Return a dictionary of information about the current active track. Dictionary keys are: - ``bit_rate`` (``int``): `Audio bit rate <https://en.wikipedia.org/wiki/Bit_rate#Encoding_bit_rate>`_ - ``channels`` (``int``): Number of `audio channels <https://en.wikipedia.org/wiki/Audio_signal>`_ - ``duration`` (``int``): Duration of the track, in milliseconds. ``0`` if unknown or none (i.e a stream) - ``file_size`` (``int``): Size of the file, in bytes. ``0`` if unknown or none (i.e a stream) - ``file_mark`` (``int``): Unknown - ``track_number`` (``int``): Track number (as stored in the audio tags). ``0`` if unknown - ``sample_rate`` (``int``): `Audio sample rate <https://en.wikipedia.org/wiki/Sampling_(signal_processing)#Sampling_rate>`_ - ``album`` (``str``): Album name or an empty string if none - ``artist`` (``str``): Artist name or an empty string if unknown - ``year`` (``int``): Track year or an empty string if unknown - ``filename`` (``str``): Path to the track or URL to the stream - ``genre`` (``str``): Track genre or an empty string if unknown - ``title`` (``str``): Track title or an empty string if unknown .. warning:: This method is experimental and should be used with caution. See `this issue <https://github.com/EpocDotFr/pyaimp/issues/1>`_ for more information. :rtype: dict """ mapped_file = mmapfile(None, AIMPRemoteAccessClass, MaximumSize=AIMPRemoteAccessMapFileSize) pack_format = ''.join(AIMPRemoteAccessPackFormat.values()) meta_data_raw = mapped_file.read(struct.calcsize(pack_format)) meta_data_unpacked = dict( zip(AIMPRemoteAccessPackFormat.keys(), struct.unpack(pack_format, meta_data_raw))) track_data = mapped_file.read( mapped_file.size() - mapped_file.tell()).decode('UTF-16').replace( '\x00', '') mapped_file.close() ret = { 'bit_rate': meta_data_unpacked['BitRate'], 'channels': meta_data_unpacked['Channels'], 'duration': meta_data_unpacked['Duration'], 'file_size': meta_data_unpacked['FileSize'], 'file_mark': meta_data_unpacked['FileMark'], 'track_number': meta_data_unpacked['TrackNumber'], 'sample_rate': meta_data_unpacked['SampleRate'] } with io.StringIO(track_data) as s: ret['album'] = s.read(meta_data_unpacked['AlbumLength']) ret['artist'] = s.read(meta_data_unpacked['ArtistLength']) ret['year'] = s.read(meta_data_unpacked['DateLength']) ret['filename'] = s.read(meta_data_unpacked['FileNameLength']) ret['genre'] = s.read(meta_data_unpacked['GenreLength']) ret['title'] = s.read(meta_data_unpacked['TitleLength']) return ret
def _getImage(self): # {'type': numpy.ndarray} # 0 uses internal flash signal # 1 uses internal exposure signal (PVCam and PXL only) # shutter_mode = 1 offset = dict(self.getOffset()) dimension = self.getDimension() binning = self.getBinning() camerasize = self.getCameraSize() mirror = self.getMirror() if mirror == 'vertical': offset['x'] = camerasize['x'] / binning['x'] - offset[ 'x'] - dimension['x'] elif mirror == 'horizontal': offset['y'] = camerasize['y'] / binning['y'] - offset[ 'y'] - dimension['y'] elif mirror == 'both': offset['x'] = camerasize['x'] / binning['x'] - offset[ 'x'] - dimension['x'] offset['x'] = camerasize['x'] / binning['x'] - offset[ 'x'] - dimension['x'] # rotation untested rotation = self.getRotation() if rotation == 90: offset['x'] = camerasize['y'] / binning['y'] - offset[ 'y'] - dimension['y'] offset['y'] = offset['x'] elif rotation == 180: offset['x'] = camerasize['x'] / binning['x'] - offset[ 'x'] - dimension['x'] offset['y'] = camerasize['y'] / binning['y'] - offset[ 'y'] - dimension['y'] elif rotation == 270: offset['x'] = offset['y'] offset['y'] = camerasize['x'] / binning['x'] - offset[ 'x'] - dimension['x'] cameracontrol.lock() cameracontrol.setCamera(self) hr = cameracontrol.camera.Format(offset['x'] * binning['x'], offset['y'] * binning['y'], dimension['x'], dimension['y'], binning['x'], binning['y']) exposuretype = self.getExposureType() t0 = time.time() if exposuretype == 'normal': hr = cameracontrol.camera.AcquireImage(self.getExposureTime(), 0) elif exposuretype == 'dark': hr = cameracontrol.camera.AcquireDark(self.getExposureTime(), 0) elif exposuretype == 'bias': hr = cameracontrol.camera.AcquireBias(0) elif exposuretype == 'readout': hr = cameracontrol.camera.AcquireReadout(0) else: cameracontrol.unlock() raise ValueError('invalid exposure type for image acquisition') t1 = time.time() self.exposure_timestamp = (t1 + t0) / 2.0 cameracontrol.unlock() imagesize = self.bytesperpixel * dimension['x'] * dimension['y'] map = mmapfile.mmapfile('', self.mmname, imagesize) na = numpy.fromstring(map.read(imagesize), self.imagetype) map.close() na.shape = (dimension['y'], dimension['x']) return na