Reputation: 2309
I'm using PyBluez to connect to a device through bluetooth. It is working just fine. The device is using SPP and I connect using a BluetoothSocket (RFCOMM).
Basic interaction is: Send init command -> receive confirmation; send start command -> receive continuous data; send stop command -> ...
I need to be able to control the application from another app: starting, stopping ... I was thinking ZeroMQ or maybe Tornado and a HTTP/REST ifc.
I'm not very keen on making this a multi-threaded app, as I believe it to be a bit over-kill. I've worked a lot with threads and pools of them in C#/.NET, but I have a hunch it is not really necessary here. On the contrary, I think it will be quite messy.
However, I need to be able to process commands (e.g. "start", "stop") via ZeroMQ/REST/ ... while continosly receiving data and sending the occasional packet.
As I am very new to Python I am not sure how to implement this. I have a few ideas: Can the BluetoothSocket be hooked up to the ZeroMQ/Tornado IOLoop?
I assume I can accomplish almost anything using Twisted, but I don't really need everything that Twisted provides. If I need to pull in Twisted, I'll do it. I've actually found a Twisted implementation of BluetoothSocket. But again, do I need Twisted?
I've tried using the Tornado IOLoop. No exceptions are thrown, but on the other hand no data is being received or sent:
def eventhandler(s, events, error = None):
if events & ioloop.IOLoop.READ:
print 'Socket read: %r' % s.recv(1024)
elif events & ioloop.IOLoop.ERROR:
print 'Socket error!'
events = ioloop.IOLoop.READ | ioloop.IOLoop.ERROR
self._loop.add_handler(self._socket.fileno(), eventhandler, events)
I don't really know what I'm doing right now. I'll find a way eventually, but need a hint on which direction to go.
Extensive Google-fu hasn't turned up much useful information, so I'm asking here now.
Edit: I'm currently looking at "gevent". Seems simpler than Twisted at least.
Upvotes: 3
Views: 1544
Reputation: 27098
This is a no direct experience viewpoint, however I did a bit of looking and what you need to consider in your choices is blocking vs. non-blocking IO.
From the quick read the PyBluez module (library) doesn't support async IO, which means that you will end up needing to dig deep into the library to get the read-write bits all handled correctly in either Tornado or Twisted.
If it was my project, what I would probably do is put the PyBluez library into it's own Thread and then have some queues that it interacts with back to Tornado (I'm a tornado fan). Now it's just a question of how to deliver BlueTooth events to your main IOLoop. You really have two choices, connect a socket to yourself since the IOLoop is blocked in a select typically. Or set a timer to callback every 100ms to check to see if there is any activity on the Bluetooth queue that it should deal with.
Upvotes: 2
Reputation: 31910
You should just use Twisted. Your real question seems to be about Twisted's size and resource utilization; whether you will pay an unreasonable cost in your application for using it, whether in terms of API complexity, memory, package size on disk, deployment hassles, or conflicts with other libraries you might want to use.
You won't.
I hope this clears up any misconceptions you might have had about Twisted being "overkill".
Upvotes: 3