pymab
25th February 2011, 02:42
Hi,
I'm currently experimenting with QSharedMemory and QProcess in PyQt. So I wrote a small application to launch a process. From this process I create a shared memory segment and write data to it. The application reads the data back when the process writes on the output. Unfortunately I get this error when the application attempts to attach to the shared memory segment: QSharedMemory::handle: doesn't exist.
Is it possible to do this sort of things ? Or maybe I should use a different approach to achieve this ?
Thanks for the help!
-mab
My application code:
from PyQt4 import QtGui, QtCore
import sys
import pickle
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget,self).__init__()
# create process
self.p = QtCore.QProcess(self)
# Connect to process output
self.p.readyReadStandardOutput.connect( self.on_process_output )
self.key = 'share_mem_key'
self.shmem = QtCore.QSharedMemory( key )
self.p.start( 'python.exe test_process.py "%s"' % self.key )
def on_process_output(self):
self.shmem = QtCore.QSharedMemory( self.key )
if not self.shmem.isAttached() and not self.shmem.attach():
# Error when trying to attach to the shared memory segment
print 'ERROR %s ' % self.shmem.errorString()
self.shmem.lock()
try:
print pickle.loads(self.shmem.data().asstring())
finally:
self.shmem.unlock()
# application loop
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()
The external process code:
from PyQt4 import QtCore
import ctypes
import ctypes.util
import pickle
import sys
CLIB = ctypes.cdll.LoadLibrary(ctypes.util.find_library(' c'))
def main(argv):
key = argv[1]
# write to shared memory
data = range(50000)
data_bytes = pickle.dumps( data, pickle.HIGHEST_PROTOCOL)
data_len = len(data_bytes)
shmem = QtCore.QSharedMemory(key)
if not shmem.create(data_len):
sys.stderr.write( 'ERROR: shared memory creation' )
sys.stderr.flush()
return
if not shmem.isAttached() and not shmem.attach():
sys.stderr.write( 'ERROR: shared memory access' )
sys.stderr.flush()
return
shmem.lock()
try:
CLIB.memcpy(int(shmem.data()), data_bytes, data_len)
finally:
shmem.unlock()
sys.stdout.write( 'done' )
sys.stdout.flush()
if __name__ == '__main__':
main(sys.argv)
I'm currently experimenting with QSharedMemory and QProcess in PyQt. So I wrote a small application to launch a process. From this process I create a shared memory segment and write data to it. The application reads the data back when the process writes on the output. Unfortunately I get this error when the application attempts to attach to the shared memory segment: QSharedMemory::handle: doesn't exist.
Is it possible to do this sort of things ? Or maybe I should use a different approach to achieve this ?
Thanks for the help!
-mab
My application code:
from PyQt4 import QtGui, QtCore
import sys
import pickle
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget,self).__init__()
# create process
self.p = QtCore.QProcess(self)
# Connect to process output
self.p.readyReadStandardOutput.connect( self.on_process_output )
self.key = 'share_mem_key'
self.shmem = QtCore.QSharedMemory( key )
self.p.start( 'python.exe test_process.py "%s"' % self.key )
def on_process_output(self):
self.shmem = QtCore.QSharedMemory( self.key )
if not self.shmem.isAttached() and not self.shmem.attach():
# Error when trying to attach to the shared memory segment
print 'ERROR %s ' % self.shmem.errorString()
self.shmem.lock()
try:
print pickle.loads(self.shmem.data().asstring())
finally:
self.shmem.unlock()
# application loop
app = QtGui.QApplication(sys.argv)
widget = Widget()
widget.show()
app.exec_()
The external process code:
from PyQt4 import QtCore
import ctypes
import ctypes.util
import pickle
import sys
CLIB = ctypes.cdll.LoadLibrary(ctypes.util.find_library(' c'))
def main(argv):
key = argv[1]
# write to shared memory
data = range(50000)
data_bytes = pickle.dumps( data, pickle.HIGHEST_PROTOCOL)
data_len = len(data_bytes)
shmem = QtCore.QSharedMemory(key)
if not shmem.create(data_len):
sys.stderr.write( 'ERROR: shared memory creation' )
sys.stderr.flush()
return
if not shmem.isAttached() and not shmem.attach():
sys.stderr.write( 'ERROR: shared memory access' )
sys.stderr.flush()
return
shmem.lock()
try:
CLIB.memcpy(int(shmem.data()), data_bytes, data_len)
finally:
shmem.unlock()
sys.stdout.write( 'done' )
sys.stdout.flush()
if __name__ == '__main__':
main(sys.argv)