Search code examples
pythonpython-3.xmultithreadingpython-multithreading

Is it possible to run 2 threads in the same time continuously in python?


I try to run a thread that is calculating different data and when the server call is made to serve data.

What i do not understand is why the program not pass to call sending and receiving after thread start

class FuncThread(threading.Thread):
  def __init__(self, image_model):  
    self.image_model = image_model
    threading.Thread.__init__(self)

  def run(self):
    image_model = self.image_model
    while True:

def sending_ receiving(): 
  while true: 

image_model = init()
thread1 = FuncThread(image_model)  
thread1.setDaemon(True)
thread1.start() # this should not affect the course of executing order 
sending_and_reciveing()   - this is contiuously listening client request

thread.start is calling run method that is a while true loop that run continuously .


Solution

  • if I correct the typos in your code, it works well on my machine.

    import time
    import threading
    
    class FuncThread(threading.Thread):
      def __init__(self, image_model):  
        self.image_model = image_model
        threading.Thread.__init__(self)
    
      def run(self):
        image_model = self.image_model
        while True:
            print('run')
            time.sleep(1)
    
    def sending_and_receiving(): 
      while True: 
          print('sending_and_receiving')
          time.sleep(1)
    
    image_model = 'test'
    thread1 = FuncThread(image_model)  
    thread1.setDaemon(True)
    thread1.start()
    sending_and_receiving()