繁体   English   中英

是否可以在 python 中同时连续运行 2 个线程?

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

我尝试运行一个计算不同数据的线程,以及何时调用服务器来提供数据。

我不明白的是为什么程序在线程启动后不通过调用发送和接收

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 正在调用 run 方法,该方法是一个持续运行的 while true 循环。

如果我更正代码中的拼写错误,它在我的机器上运行良好。

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() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM