Results 1 to 3 of 3

Thread: QSharedMemory::handle: doesn't exist

  1. #1
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Platforms
    Windows

    Default QSharedMemory::handle: doesn't exist

    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:
    Qt Code:
    1. from PyQt4 import QtGui, QtCore
    2. import sys
    3. import pickle
    4.  
    5. class Widget(QtGui.QWidget):
    6. def __init__(self):
    7. super(Widget,self).__init__()
    8.  
    9. # create process
    10. self.p = QtCore.QProcess(self)
    11.  
    12. # Connect to process output
    13. self.p.readyReadStandardOutput.connect( self.on_process_output )
    14.  
    15. self.key = 'share_mem_key'
    16. self.shmem = QtCore.QSharedMemory( key )
    17.  
    18. self.p.start( 'python.exe test_process.py "%s"' % self.key )
    19.  
    20. def on_process_output(self):
    21. self.shmem = QtCore.QSharedMemory( self.key )
    22. if not self.shmem.isAttached() and not self.shmem.attach():
    23. # Error when trying to attach to the shared memory segment
    24. print 'ERROR %s ' % self.shmem.errorString()
    25.  
    26. self.shmem.lock()
    27. try:
    28. print pickle.loads(self.shmem.data().asstring())
    29. finally:
    30. self.shmem.unlock()
    31.  
    32. # application loop
    33. app = QtGui.QApplication(sys.argv)
    34. widget = Widget()
    35.  
    36. widget.show()
    37. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    The external process code:
    Qt Code:
    1. from PyQt4 import QtCore
    2. import ctypes
    3. import ctypes.util
    4. import pickle
    5. import sys
    6.  
    7. CLIB = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
    8.  
    9. def main(argv):
    10. key = argv[1]
    11.  
    12. # write to shared memory
    13. data = range(50000)
    14. data_bytes = pickle.dumps( data, pickle.HIGHEST_PROTOCOL)
    15. data_len = len(data_bytes)
    16.  
    17. shmem = QtCore.QSharedMemory(key)
    18. if not shmem.create(data_len):
    19. sys.stderr.write( 'ERROR: shared memory creation' )
    20. sys.stderr.flush()
    21. return
    22.  
    23. if not shmem.isAttached() and not shmem.attach():
    24. sys.stderr.write( 'ERROR: shared memory access' )
    25. sys.stderr.flush()
    26. return
    27.  
    28. shmem.lock()
    29. try:
    30. CLIB.memcpy(int(shmem.data()), data_bytes, data_len)
    31. finally:
    32. shmem.unlock()
    33.  
    34. sys.stdout.write( 'done' )
    35. sys.stdout.flush()
    36.  
    37. if __name__ == '__main__':
    38. main(sys.argv)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedMemory::handle: doesn't exist

    Does the shared memory block exist when the parent process tries to access it? Have you verified the child process successfully creates it? Which platform you are using, Windows? I don't see that Qt returns such a message for Windows anywhere, only for UNIX.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Platforms
    Windows

    Default Re: QSharedMemory::handle: doesn't exist

    Quote Originally Posted by wysota View Post
    Does the shared memory block exist when the parent process tries to access it?
    Apparently no, I get this error when trying to access it from the output callback:
    : QSharedMemory::handle: doesn't exist

    Quote Originally Posted by wysota View Post
    Have you verified the child process successfully creates it?
    Yes, everything is ok since I don't get an error msg in the process error callback.

    Quote Originally Posted by wysota View Post
    Which platform you are using, Windows? I don't see that Qt returns such a message for Windows anywhere, only for UNIX.
    I'm on Windows. Here's the output trace (from my updated code below):
    <pre>
    process state: Starting
    process state: Running
    process started
    process output: Done writing to shared memory
    Error accessing shared memory from parent process QSharedMemory::handle: doesn't exist
    Error accessing data
    process state: Not running
    process finished
    </pre>

    App:
    python Code:
    1. from PyQt4 import QtGui, QtCore
    2. import sys
    3. import pickle
    4.  
    5. class Widget(QtGui.QWidget):
    6. def __init__(self):
    7. super(Widget,self).__init__()
    8.  
    9. # create process
    10. self.p = QtCore.QProcess(self)
    11.  
    12. # Connect to process output
    13. self.p.readyReadStandardOutput.connect( self.on_process_output )
    14. self.p.readyReadStandardError.connect( self.on_process_error_output )
    15. self.p.stateChanged.connect(self.on_process_state_change)
    16. self.p.finished.connect(self.on_process_finished)
    17. self.p.started.connect(self.on_process_started)
    18. self.p.error.connect( self.on_process_error )
    19.  
    20. self.key = 'share_mem_key'
    21. self.shmem = QtCore.QSharedMemory( self.key )
    22.  
    23. self.p.start( 'python.exe shmem_process_test.py "%s"' % self.key )
    24.  
    25. def on_process_output(self):
    26. s_out = bytes.decode( bytes( self.sender().readAllStandardOutput() ) )
    27. print 'process output: %s' % (s_out)
    28.  
    29. if not self.shmem.isAttached() and not self.shmem.attach():
    30. print 'Error accessing shared memory from parent process: %s ' % self.shmem.errorString()
    31.  
    32. self.shmem.lock()
    33. try:
    34. data = self.shmem.data()
    35. if data:
    36. print pickle.loads(data.asstring())
    37. finally:
    38. print 'Error accessing data'
    39. self.shmem.unlock()
    40.  
    41. def on_process_error_output(self):
    42. s_out = bytes.decode( bytes( self.sender().readAllStandardError() ) )
    43. print 'process output: %s' % (s_out)
    44.  
    45. def on_process_state_change(self,new_state):
    46. states = ["Not running", "Starting", "Running"]
    47. print 'process state: %s' % (states[new_state])
    48.  
    49. def on_process_finished(self):
    50. print 'process finished'
    51.  
    52. def on_process_started(self):
    53. print 'process started'
    54.  
    55. def on_process_error(self):
    56. print 'process error'
    57.  
    58. # application loop
    59. app = QtGui.QApplication(sys.argv)
    60. widget = Widget()
    61.  
    62. widget.show()
    63. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    Process:
    python Code:
    1. from PyQt4 import QtCore
    2. import ctypes
    3. import ctypes.util
    4. import pickle
    5. import sys
    6.  
    7. CLIB = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
    8.  
    9. def main(argv):
    10. key = argv[1]
    11.  
    12. # write to shared memory
    13. data = range(50)
    14. data_bytes = pickle.dumps( data, pickle.HIGHEST_PROTOCOL)
    15. data_len = len(data_bytes)
    16.  
    17. shmem = QtCore.QSharedMemory(key)
    18.  
    19. if not shmem.create(data_len):
    20. sys.stderr.write( 'ERROR: shared memory creation' )
    21. sys.stderr.flush()
    22. return
    23.  
    24. if not shmem.isAttached() and not shmem.attach():
    25. sys.stderr.write( 'ERROR: shared memory access' )
    26. sys.stderr.flush()
    27. return
    28.  
    29. shmem.lock()
    30. try:
    31. CLIB.memcpy(int(shmem.data()), data_bytes, data_len)
    32. finally:
    33. shmem.unlock()
    34.  
    35. sys.stdout.write( "Done writing to shared memory" )
    36. sys.stdout.flush()
    37.  
    38. if __name__ == '__main__':
    39. main(sys.argv)
    To copy to clipboard, switch view to plain text mode 

    So it seems like the only way the parent can successfully access the shared memory is if the memory is allocated by itself before starting the child process. This is not what I want. What I want is to let the child process allocate the memory with the right size instead of allocating upfront in the parent process.

    Thanks for looking at this.
    -mab
    Last edited by wysota; 27th February 2011 at 16:44. Reason: missing [code] tags

Similar Threads

  1. Qt Creator: File Makefile doesn't exist
    By earthling in forum Qt Tools
    Replies: 6
    Last Post: 4th November 2016, 03:44
  2. Replies: 13
    Last Post: 4th November 2010, 22:34
  3. about the QSharedMemory
    By banban0802 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2010, 12:08
  4. Qt Creator: File Makefile doesn't exist
    By earthling in forum Qt Programming
    Replies: 0
    Last Post: 9th December 2009, 17:46
  5. qtdemo doesn't exist...
    By xyzt in forum Newbie
    Replies: 2
    Last Post: 24th March 2008, 05:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.