[LightBlue-devel] Use of lightblue in multithreaded applications
Status: Alpha
Brought to you by:
blammit
|
From: Lukas H. <Lu...@gm...> - 2010-01-30 10:34:14
|
Hello,
I wanted to port my application to Mac OS X.
This application uses PyQt4 for the user interface and has a seperate thread
to manage a bluetooth socket (using pybluez on Windows/Linux and now I want to
port this to OS X too).
The seperate thread is required, because otherwise the user interface will get
blocked.
Here is a simple example of the thing I want to make:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import lightblue
class Thread(QThread):
def run(self):
sock = lightblue.socket()
print "connecting"
sock.connect(('00:1B:AF:84:FF:E2', 18))
print "connected"
class Button(QPushButton):
def __init__(self):
QPushButton.__init__(self, "Start thread")
self.connect(self, SIGNAL("clicked()"), self.start)
def start(self):
self.thread = Thread()
self.thread.start()
app = QApplication([])
button = Button()
button.show()
app.exec_()
This didn't work, I get many of these error messages:
2010-01-30 11:25:35.909 Python[217:4c2f] *** __NSAutoreleaseNoPool(): Object
0x105b30b80 of class NSConcreteMutableData autoreleased with no pool in place
- just leaking
2010-01-30 11:25:35.912 Python[217:4c2f] *** __NSAutoreleaseNoPool(): Object
0x105b25cd0 of class NSConcreteMutableData autoreleased with no pool in place
- just leaking
2010-01-30 11:25:35.915 Python[217:4c2f] *** __NSAutoreleaseNoPool(): Object
0x102df2010 of class NSConcreteMutableData autoreleased with no pool in place
- just leaking
2010-01-30 11:25:35.917 Python[217:4c2f] *** __NSAutoreleaseNoPool(): Object
0x105b080c0 of class NSConcreteMutableData autoreleased with no pool in place
- just leaking
So i thought adding a seperate NSAutoreleasePool in my thread would solve the
problem:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import lightblue
import Foundation
class Thread(QThread):
def run(self):
pool = Foundation.NSAutoreleasePool.alloc().init()
sock = lightblue.socket()
print "connecting"
sock.connect(('00:1B:AF:84:FF:E2', 18))
print "connected"
pool.release()
class Button(QPushButton):
def __init__(self):
QPushButton.__init__(self, "Start thread")
self.connect(self, SIGNAL("clicked()"), self.start)
def start(self):
self.thread = Thread()
self.thread.start()
app = QApplication([])
button = Button()
button.show()
app.exec_()
But now the sock.connect hangs forever:
lukass-Mac:~ Lukas$ python test.py
/Library/Python/2.6/site-packages/lightblue/_bluetoothsockets.py:42:
DeprecationWarning: the sets module is deprecated
import sets # python 2.3
connecting
I know that the IOBluetooth framework isn't multithreadable, but is there no
workaround for this (like using the bluetooth socket in only one thread)?
Is there any example for this task?
Is it really not possible to make an application using lightblue which has a
non-blocking user interface?
I'm using Mac OS X 10.6 Snow Leopard.
Python: 2.6.1
lightblue: 0.4 with OS X 10.6 patch
PyQt4: 4.7
Thank you for any help,
Lukas Hetzenecker
|