首页 > 解决方案 > 将函数中的循环设置为不同的线程python

问题描述

如果我有一个循环将一个大文件发送到服务器,并且我仍然想通过相同的函数和套接字向服务器发送信息而不中断文件发送,我将如何设置循环在不同的线程或进程上运行

 def sendtoserver():

 # send a string here without interrupting 

 while not flsize == 0:  # while loop multithread


                            with open(fp, "rb") as f:
                                for _ in progress:
                                    # read the bytes from the file
                                    bytes_read = f.read(flsize)   
                                    bytes_read_small = f.read(flsize)

                                    if not bytes_read:
                                        # file transmitting is done
                                        break

                                    self.s.sendall(bytes_read)

                                    # update the progress bar
                                    progress.update(len(bytes_read))

                                    global file_sent
                                    file_sent = 1
                                    flsize = 1
                                    self.s.shutdown()
                                    self.s.close()

标签: python

解决方案


推荐阅读