简体   繁体   English

如何创建多个线程

[英]How can I create multiple threads

I am working on python 3 and my class is as below.我正在研究 python 3,我的课程如下。

class MyClass():
     def values(self):
        ***values***

     i =0
     def check_values(self):
         for i in ValueList[i:i+1]:
              self.server_connect()
              new_value = self.update.values(i)

      def run(self):
          self.check_values()

if __name__ == "__main__"
     format1 = "%(asctime)s: %(message)s"
     logging.basicConfig(format=format1, level=logging.INFO,
                        datefmt="%H:%M:%S")

     for i in range(4):
          thread = threading.Thread(target=MyClass().run())
          threads.append(thread)
          i += 1
          print("the %s thread is running", thread)
          thread.start()

There are no threads getting created but code works.没有创建线程,但代码有效。 I am not able to catch what I am doing wrong here.我无法发现我在这里做错了什么。

EDIT编辑

First, I would like to thank you for response and time given for the answer.首先,我要感谢您的答复和时间给予答复。 I have to update code and inherit other class as per new update from team as below.我必须更新代码并根据团队的新更新继承其他类,如下所示。

class MyClass(MainServer):

Now, the server has it's own run function as below.现在,服务器有自己的运行功能,如下所示。

class MainServer(object):
   ***constructor***
   ***other functions ***
     def run(self):
         self.add_arguments()
         self.parse_arguments()
         self.check_values()

Now, without run(), my code is not properly running.现在,如果没有 run(),我的代码无法正常运行。 while including run() as below.同时包括 run() 如下。

*** main ***
update_perform = MyClass()
for i range(4):
     thread = threading.Thread(target=Myclass().run()) <-- code starts from here 
     threads.append(thread)
     i += 1
     print("the %s thread is running", thread)
     thread.start()    <-- not reaching till here

As per my knowledge I will require thread.start() to start threading.据我所知,我将需要 thread.start() 来启动线程。 So I have tried below option所以我尝试了以下选项

class MyClass(MainServer):
     ***code as above***
   def check_values(self):
        self.server_authenticate()
        update_value = self.update.values()

    def run(self):
         self.server_connect()
         i = 0
         threads = list()
         for i in ValueList[i:i+1]:
             print("Updating the value = ", i)
             thread = threading.Thread(target=check_values(), args=[i])
             thread.start()
             i += 1
             print("Currently running thread", thread)
             threads.append(thread)
         for thread in threads:
              thread.join()

Here thread is executing from start and in print I can see as below这里的线程从开始和打印中执行,我可以看到如下

for threading :- 
Currently running threads = <Thread(Thread-8, stopped 14852)>

But for the value I can see only one is in process as below但是对于我可以看到的价值,只有一个正在处理中,如下所示

for value :- 
Updating the value = 10  <- first value 

So, now threads may be getting created but the values are not getting executed in parallel.因此,现在可能会创建线程,但不会并行执行这些值。

Which I am not able to figure out.我无法弄清楚。

modify the run function like this像这样修改运行函数

def run(self):
   self.check_values()

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

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