python multithreading write to file

python multithreading write to file

In python, the operation of the file is very simple, generally by opening the file, obtaining the file object, and then writing the file object.

Here are some common methods for file:

class file(object):
   
    def close(self): # real signature unknown; restored from __doc__
        
        pass

    def fileno(self): # real signature unknown; restored from __doc__
       
        return 0

    def flush(self): # real signature unknown; restored from __doc__
        
        pass

    def isatty(self): # real signature unknown; restored from __doc__
        
        return False

    def next(self): # real signature unknown; restored from __doc__
        
        pass

    def read(self, size=None): # real signature unknown; restored from __doc__
       
        pass

    def readinto(self): # real signature unknown; restored from __doc__
       
        pass

    def readline(self, size=None): # real signature unknown; restored from __doc__
       
        pass

    def readlines(self, size=None): # real signature unknown; restored from __doc__
       
        return []

    def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
       
        pass

    def tell(self): # real signature unknown; restored from __doc__
        """ tell() -> current file position, an integer (may be a long integer). """
        pass

    def truncate(self, size=None): # real signature unknown; restored from __doc__
       
        pass

    def write(self, p_str): # real signature unknown; restored from __doc__
        
        pass

    def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
       
        pass

    def xreadlines(self): # real signature unknown; restored from __doc__
       
        pass

Usually, it is written directly one by one through write. If one day, you suddenly want multiple threads to write a file, what should we do? ?

The idea is this:

    1. Determine the size of the file

    2. And divide the size of the content that needs to be processed for each thread

    3. Then write the content into each thread

    4. It should be noted that for each thread, to give a file object, usually open the file in the main thread to obtain the file object, then copy the descriptor of the file object, and use os.fdopen() to obtain the file object, pass Use it for each thread!




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325873537&siteId=291194637