Print output from Python code never appeared

I had started my Python script with output redirection to a file with |tee

pipenv run python main.py|tee -a run.out
The very first print statement that I had in my code that is printed upon program startup did get printed. After this nothing at all got printed. I do some printing before sending a REST API call. Strangely the REST API call had gotten called on the receiver (which is a server running on the same machine) but nothing ever got printed from the Python script. Also the receiver code received the REST API call 18 seconds later than I would have expected (I was having the print statements in Python code to let me know the exact timestamp certain events occurred which trigger my processing, and these events could not have gotten delayed by an entire 18 seconds). When I pressed Ctrl-C to terminate the Python script, a message about a broken pipe appeared on the screen:

pipenv run python main.py|tee -a run.out

05-05-2024-15:56:48: Started.

^C

Exception ignored in: <_io.TextIOWrapper name=” mode=’w’
encoding=’utf-8′> BrokenPipeError: [Errno 32] Broken pipe

Here is my code:

#!/usr/bin/python3

from pyrogram import Client, filters, idle
from pyrogram.handlers import MessageHandler
import config
from pytz import timezone
from datetime import datetime
import requests
import asyncio

CHANNEL_ID = 12345
has_moment_arrived = {}

def india_number_handler(client, message):
    message_handler('india_number', message)
    
def uk_number_handler(client, message):
    message_handler('uk_number', message) 
    
def message_handler(session_name, message):
    global has_moment_arrived
    if message.sender_chat is not None and message.sender_chat.id == CHANNEL_ID and message.text is not None:
        print(datetime.now().strftime('%d-%m-%Y-%H:%M:%S-%f') + ': ' + session_name + ' received crypto pump channel message: ' + message.text)
        if has_moment_arrived[session_name]:
            print('Handling stuff ' + message.text)
            handle_stuff(message.text)
            has_moment_arrived[session_name] = False
        elif 'Next message is the moment.' in message.text:
            print('Expecting to receive our stuff in the next message')
            has_moment_arrived[session_name] = True
    
def handle_stuff(message):
    headers = {
      'Content-Type': 'text/plain'
    }
    requests.post("http://localhost:8080/postMessage", headers=headers, data=message)
    
async def main():
    global has_moment_arrived
    print('nn' + datetime.now().strftime('%d-%m-%Y-%H:%M:%S') + ': Started.', flush = True)
    has_moment_arrived['india_number'] = False
    has_moment_arrived['uk_number'] = False
    app = Client("india_number", api_id=config.getEnv("api_id"), api_hash=config.getEnv("api_hash"))
    app_uk = Client("uk_number", api_id=config.getEnv("api_id"), api_hash=config.getEnv("api_hash"))
    app.add_handler(MessageHandler(india_number_handler))
    app_uk.add_handler(MessageHandler(uk_number_handler))    
    await app.start()
    await app_uk.start()
    await idle()
    await app.stop()
    await app_uk.stop()
         
asyncio.run(main())

Except the starting print statement, none of my other prints had a flush=True. Can someone help me understand why the print outputs vanished, and more importantly why the following statements that made the REST API call got delayed?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật