Summary of errors using Python multithreading

Source: Internet
Author: User
Tags variable scope

When using Python multi-threading, when using multithreaded programming, because of the variable scope and multithreading is not very familiar, resulting in the use of multi-threaded, made a low-level error.

First error:

Using global variables in multi-threading causes multiple threads to modify global variables. The execution of information disorder begins with a few processes, and then becomes one. Later, after re-learning multi-threading, the original error is changed over.

Scripting features, multi-threaded upload and download files to devices, test FTP functionality and performance. The reason for the error is to set the FTP variable to a global variable, resulting in a strange phenomenon, starting with a few threads running, then a few process exits, and finally into a, and a hint of the FTP password error occurred. Debugging for a long time, later using pycharm tools, observed the cause of the problem.

The code is as follows:

The red code is the wrong version, initially, the FTP variable is outside, and is used as a global variable. The green code is to modify the correct version.

#!/usr/bin/env python#Coding=utf-8 fromFtplibImportFTP fromDatetimeImportdatetimeImportSYSImportOSImportThreadingftp_port=' +'Telnet_port=' at'buffsize=1024#ftp =ftp ()classFtp_test (Threading. Thread): Upload_dir=".. /upload/"Download_dir=".. /download"IP="'Username="'Password="'            def __init__(Self,env_para): Threading. Thread.__init__(self) self. IP=env_para['ip_addr'] Self. Password= env_para['Password'] Self. Username= env_para['Admin'] Self.upload_dir= env_para['Upload_dir'] Self.download_dir= env_para['Download_dir']    defftp_upload (self,tfile): ftp=ftp () Ftp.connect (self. IP, Ftp_port,timeout=10) Ftp.login (self. Username,self. Password)#print ftp.getwelcome ()ftp.cwd (RAMDisk)#print Ftp.dir ()File_handler=open (Self.upload_dir + tfile,'RB') Ftp.storbinary ('STOR'+Tfile, File_handler,buffsize)#Ftp.dir ()file_handler.close () ftp.quit ( )PrintTfile,'Upload OK'    defftp_download (self,t_file): ftp=ftp () Ftp.connect (self. IP, Ftp_port,timeout=10)        #Ftp.set_debuglevel (2)Ftp.login (self. Username,self. Password) filename= T_file +'_download'File_write=open (self.download_dir + filename,'WB'). Write Ftp.retrbinary ('RETR'+filename, file_write, buffsize) ftp.delete (filename) ftp.quit ()PrintT_file,'FTP Download OK'    defRun (self): file_list=Os.listdir (Self.upload_dir) forEach_fileinchfile_list:Try: Self.ftp_upload (each_file)exceptException, E:PrintEach_file,'FTP Upload fail'                PrinteTry: Self.ftp_download (each_file)exceptException, E:PrintEach_file,'FTP Download Fail'                PrintE

This defines the function, reduces the scope of the FTP variable, and finally completes the FTP parallel upload and download.

Positioning process:

When using Pycharm debugging, observe the changes of the FTP variable, found that there is only one FTP variable, all processes are using this variable, the FTP variable record FTP state is constantly changing, there are a variety of strange phenomena.

After you have narrowed the scope of the FTP variable, re-debug and observe that the FTP variable is different in each ongoing address, and that each FTP change is not affected by other processes.

A second error:

Because on the network to learn to share the multi-threaded article, affected by http://www.cnblogs.com/fnng/p/3670789.html this sharing, after the thread started, directly wrote the T.join (), not all the process has been added to the join.

The process that causes the slow execution is terminated directly by the main thread, and the threads exit when the FTP does not execute more than once.

The correct wording is:

For T in Threads:

T.join ()

This analysis at that time to kill me, debugging the long time only to find this error.

Summary experience:

For learning or to read the book System of Learning.

In addition, learn to use tool debugging, observe variable changes, in-depth understanding of the program operation, easy to locate the problem.

Summary of errors using Python multithreading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.