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?