Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5941521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:20+00:00 2026-05-22T16:07:20+00:00

I currently use twisted to connect to a serial device I have, using code

  • 0

I currently use twisted to connect to a serial device I have, using code like the following.

from twisted.internet import reactor
SerialPort(Handler(), "/dev/ttyACM1", reactor, baudrate='9600')
reactor.run()

However, I know need to extend the application to monitor for new serial devices being added (and removed). I currently use pyinotify to look for new devices being added/removed, and this seems to work well.

class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        ... connect to serial device
        ...

At the moment I seem to have several problems. The biggest I think is that I just don’t know enough about Twisted to know what the ‘correct’ way of doing this is.

Currently as I have it, the pyinotify event handler kicks of in a separate thread, meaning the reactor is not running in the main thread. Is this an issue?

Once the first device is connected, I struggle to add a second – not least the reactor is already running by the time the second device is added. Even if I protect that with a

if not reactor.running: 

the second connection doesn’t add to the reactor properly (at least connectionMade, dataReceived received methods don’t fire).

If I start the reactor first, then let the pyinotify events try and add to the running reactor this doesn’t seem to work either – the device does connect, but the dataReceived method never gets called.

Basically, I’m sure there is a neat recipe for getting this to work, I just haven’t been able to find it, either through Google or trial and error. Can anybody suggest to me how I might be able to get this working?

Thanks in advance for any help you’re able to offer,

Simon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:07:20+00:00Added an answer on May 22, 2026 at 4:07 pm

    Any time you want to use a Twisted API but your code isn’t running in the same thread as the reactor, you can use reactor.callFromThread to have the reactor call some of your code in its thread. So, for example, you could do something like this:

    def process_IN_CREATE(self, event):
        reactor.callFromThread(
            SerialPort, Handler(), "/dev/ttyACM1", reactor, baudrate='9600')
    

    However, you also don’t need to have any extra threads to do what you described. Instead of using pyinotify, use twisted.internet.inotify:

    from twisted.python.filepath import FilePath
    from twisted.internet.inotify import IN_CREATE, INotify
    from twisted.internet import reactor
    
    def created(ignored, path, mask):
        SerialPort(
            Handler(),
            "/dev/ttyACM1", # Or... use `path` here?
            reactor, baudrate='9600')
    
    notifier = INotify()
    notifier.watch(FilePath("/some/directory"), IN_CREATE, callbacks=[created])
    notifier.startReading()
    
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently use the following code to retrieve and decompress string data from Amazon
I want to transmit data from a Queue using Twisted. I currently use a
I currently use the following code to trigger an event from my Firefox Add-On.
I'm currently using twisted's perspective broker on python and I have considered in the
I currently use FixedThreadPool to download images from the web, like this: ExecutorService mThreadPool
I currently use the following code to print a double: return String.format(%.2f, someDouble); This
I currently use the following code to upload one file to a remote server:
I currently use SQL2008 where I have a stored procedure that fetches data from
I currently use a DataTable to get results from a database which I can
I currently use the following command, but it's a little unwieldy to type. What's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.