Call coroutine within Thread

Viewed 4489

Is it possible to make a thread run method async so it can execute coroutines inside it? I realise I am mixing paradigms - I am trying to integrate a 3rd party library that uses coroutines whereas my project uses threads. Before I consider updating my project to use coroutines instead I'd like to explore executing coroutines within my threads.

Below is my example usecase where I have a thread but I want to call a coroutine from within my thread. My problem is the function MyThread::run() doesn't appear to be executing (printing). Is what I am trying possible...and advisable?

from threading import Thread

class MyThread(Thread):

    def __init__(self):
        Thread.__init__(self)

        self.start()

    # def run(self):
    #   while True:
    #       print("MyThread::run() sync")

    async def run(self):
        while True:
            # This line isn't executing/printing
            print("MyThread::run() sync")

            # Call coroutine...
            # volume = await market_place.get_24h_volume()


try:
    t = MyThread()

    while True:
        pass
except KeyboardInterrupt:
    pass
1 Answers
Related