How to have one thread listen for data while main thread is doing some operation

Harry Cho

I am trying to create a one thread to listen for incoming data from COM serial port while main thread is doing some operation.

Here is my code (some codes are omitted for brevity) :

def readMsg( serial ):
    msgArray = []
    while ( True ):
        char = "0x" + serial.read().encode('hex')
        if char == '0xfd':
            msgArray = []
            msgArray.append(char)
        elif char == '0xfc':
            msgArray.append(char)
            print debugPrefix, "ComPort:", (serial.port + 1), msgArray
        elif char == '0x':
            pass
        else:
            msgArray.append(char)


# Find available ports                  
initializeComPorts() 

# Print port infos
printComPorts() 

# Opens serial and returns opened serial
serialPort = openPort(1);

print "thread started"
readMsgThread = threading.Thread( target=readMsg(serialPort) )
readMsgThread.setDaemon( True )
readMsgThread.start()

print "sending some data"
serialPort.send('h')

When I execute the code readMsgThread listens fine, but the line print "sending some data" is never executed. Can someone please explain what am I missing? I've been stuck at this for a while.

Thanks you very much.

Tim Peters

I can't test this for you, but think carefully about this line:

readMsgThread = threading.Thread( target=readMsg(serialPort) )

That calls readMsg(serialPort) at the time the assignment is excecuted, and passes the result as target. I'm guessing you almost certainly want to do:

readMsgThread = threading.Thread( target=readMsg, args=(serialPort,) )

instead.

To clarify a little more, because you're calling readMsg on that line, no further lines will be executed until readMsg returns. But that function never returns. That's why "sending some data" is never seen. In fact, you never get to the

readMsgThread.setDaemon( True )

statement either.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matlab: How fork another thread to load data while the main thread keep on doing the major task?

From Java

How to melt a dataframe while doing some operation?

From Dev

Swizzling have to be on main thread?

From Dev

how to stop main thread while another thread still running

From Dev

How to not block main UI thread from a worker thread while working with a large global object on the main thread

From Dev

how to run methods in one specific thread (not main thread)

From Dev

Keeping main thread (Qt Application) responsive while some other thread is processing (POSIX)

From Dev

C# WinForms :- Why Thread Timer tick event not elapsed when UI is doing some other operation?

From Dev

How to listen a huge number of IMAP folders of differnet accounts in one thread?

From Dev

How can doing tasks in multiple threads be 100 times slower than doing sequentially on the main thread?

From Dev

How to interupt a thread blocked by some socket IO operation without closing it

From Dev

Running code in main thread in background thread while blocking background thread

From Dev

Running code in main thread in background thread while blocking background thread

From Dev

Will pthread_cancel cancel a thread while the thread is doing IO?

From Dev

Will pthread_cancel cancel a thread while the thread is doing IO?

From Dev

How to create an indeterminate progress and start a thread in background and again do some operation after the thread is complete in python

From Dev

How can I run an Async IO operation that doesn't block the main thread but throws an exception in the main thread if it fails?

From Java

How to check if current thread is not main thread

From Dev

How to fixed "The application may be doing too much work on its main thread" (Android)

From Dev

How to avoid The application may be doing too much work on its main thread

From Dev

How to limit number of threads created and wait main thread until any one thread finds answer?

From Dev

How to Reuse OMP Thread Pool, Created by Main Thread, in Worker Thread?

From Dev

Do I have to signal an anonymous thread to exit if the main thread is done?

From Dev

PyQt5: how to make QThread return data to main thread

From Dev

How to handle data returned from thread functions in main function in Python

From Dev

PyQt5: how to make QThread return data to main thread

From Dev

"NetworkOnMainThreadException", but have no network call on main thread

From Dev

iOS application have multiple main thread

From Dev

Unblock main thread while waiting for another thread to finish it's work

Related Related

  1. 1

    Matlab: How fork another thread to load data while the main thread keep on doing the major task?

  2. 2

    How to melt a dataframe while doing some operation?

  3. 3

    Swizzling have to be on main thread?

  4. 4

    how to stop main thread while another thread still running

  5. 5

    How to not block main UI thread from a worker thread while working with a large global object on the main thread

  6. 6

    how to run methods in one specific thread (not main thread)

  7. 7

    Keeping main thread (Qt Application) responsive while some other thread is processing (POSIX)

  8. 8

    C# WinForms :- Why Thread Timer tick event not elapsed when UI is doing some other operation?

  9. 9

    How to listen a huge number of IMAP folders of differnet accounts in one thread?

  10. 10

    How can doing tasks in multiple threads be 100 times slower than doing sequentially on the main thread?

  11. 11

    How to interupt a thread blocked by some socket IO operation without closing it

  12. 12

    Running code in main thread in background thread while blocking background thread

  13. 13

    Running code in main thread in background thread while blocking background thread

  14. 14

    Will pthread_cancel cancel a thread while the thread is doing IO?

  15. 15

    Will pthread_cancel cancel a thread while the thread is doing IO?

  16. 16

    How to create an indeterminate progress and start a thread in background and again do some operation after the thread is complete in python

  17. 17

    How can I run an Async IO operation that doesn't block the main thread but throws an exception in the main thread if it fails?

  18. 18

    How to check if current thread is not main thread

  19. 19

    How to fixed "The application may be doing too much work on its main thread" (Android)

  20. 20

    How to avoid The application may be doing too much work on its main thread

  21. 21

    How to limit number of threads created and wait main thread until any one thread finds answer?

  22. 22

    How to Reuse OMP Thread Pool, Created by Main Thread, in Worker Thread?

  23. 23

    Do I have to signal an anonymous thread to exit if the main thread is done?

  24. 24

    PyQt5: how to make QThread return data to main thread

  25. 25

    How to handle data returned from thread functions in main function in Python

  26. 26

    PyQt5: how to make QThread return data to main thread

  27. 27

    "NetworkOnMainThreadException", but have no network call on main thread

  28. 28

    iOS application have multiple main thread

  29. 29

    Unblock main thread while waiting for another thread to finish it's work

HotTag

Archive