Inconsistent TypeError: cannot serialize '_io.TextIOWrapper' object

the problem is that you should avoid same names for different objects, in your case should help

  • changing function name from f to function (or another name different from f)

    cell 1

    from time import time
    from multiprocessing import Pool
    import portalocker
    
    
    def function(*args):
        while time() < start + 1:
            pass
        with open('portalocker_test.txt', 'a') as f:
            portalocker.lock(f, portalocker.LOCK_EX)
            f.write(f'{time()}\n')
    

    cell 2

    start = time()
    with Pool(4) as p:
        p.map(function, range(4))
    

or

  • renaming file objects obtained with open from f to file (or another name different from f):

    cell 1

    from time import time
    from multiprocessing import Pool
    import portalocker
    
    
    def f(*args):
        while time() < start + 1:
            pass
        with open('portalocker_test.txt', 'a') as file:
            portalocker.lock(file, portalocker.LOCK_EX)
            file.write(f'{time()}\n')
    

    cell 3

    with open('portalocker_test.txt', 'r') as file:
        for line in file:
            print(line, end='')
    

or both