Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,7 +80,7 @@ def get_media_obj(msg):
|
|
| 80 |
return None
|
| 81 |
|
| 82 |
def get_msg_file_id(msg):
|
| 83 |
-
"""মেসেজ থেকে ক্র্যাশ-ফ্রিfile_id বের করার ফাংশন"""
|
| 84 |
if not msg:
|
| 85 |
return None
|
| 86 |
if msg.photo:
|
|
@@ -101,27 +101,37 @@ def get_file_stream(message_id):
|
|
| 101 |
msg = await bot.get_messages(STORAGE_CHANNEL_ID, message_id)
|
| 102 |
media = get_media_obj(msg)
|
| 103 |
if not media:
|
| 104 |
-
q.put
|
| 105 |
return
|
| 106 |
|
| 107 |
-
# ডাটা রিড করে থ্রেড-সেফ কিউ-তে রাখা হচ্ছে
|
| 108 |
async for chunk in bot.stream_media(media):
|
| 109 |
-
q.put
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error in stream producer: {e}")
|
| 112 |
finally:
|
| 113 |
-
q.put
|
| 114 |
|
| 115 |
# প্রোডিউসারটিকে বটের নিজের মেইন ইভেন্ট লুপে রান করানো হলো
|
| 116 |
asyncio.run_coroutine_threadsafe(producer(), main_loop)
|
| 117 |
|
| 118 |
# ফ্লাস্কের জন্য সিনক্রোনাস কন্সুমার জেনারেটর
|
| 119 |
def consumer():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
return consumer()
|
| 127 |
|
|
@@ -996,7 +1006,7 @@ async def catch_admin_steps(client, message):
|
|
| 996 |
elif state.get("step") == "broadcast":
|
| 997 |
await process_broadcast(client, message)
|
| 998 |
|
| 999 |
-
def run_flask(): app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
| 1000 |
|
| 1001 |
async def main():
|
| 1002 |
await bot.start()
|
|
|
|
| 80 |
return None
|
| 81 |
|
| 82 |
def get_msg_file_id(msg):
|
| 83 |
+
"""মেসেজ থেকে ক্র্যাশ-ফ্রি file_id বের করার ফাংশন"""
|
| 84 |
if not msg:
|
| 85 |
return None
|
| 86 |
if msg.photo:
|
|
|
|
| 101 |
msg = await bot.get_messages(STORAGE_CHANNEL_ID, message_id)
|
| 102 |
media = get_media_obj(msg)
|
| 103 |
if not media:
|
| 104 |
+
await asyncio.to_thread(q.put, None)
|
| 105 |
return
|
| 106 |
|
| 107 |
+
# ডাটা রিড করে থ্রেড-সেফ কিউ-তে রাখা হচ্ছে (asyncio.to_thread ব্যবহারের ফলে মেইন লুপ কখনো ফ্রিজ হবে না)
|
| 108 |
async for chunk in bot.stream_media(media):
|
| 109 |
+
await asyncio.to_thread(q.put, chunk)
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error in stream producer: {e}")
|
| 112 |
finally:
|
| 113 |
+
await asyncio.to_thread(q.put, None) # স্ট্রিম শেষ হওয়ার সিগন্যাল
|
| 114 |
|
| 115 |
# প্রোডিউসারটিকে বটের নিজের মেইন ইভেন্ট লুপে রান করানো হলো
|
| 116 |
asyncio.run_coroutine_threadsafe(producer(), main_loop)
|
| 117 |
|
| 118 |
# ফ্লাস্কের জন্য সিনক্রোনাস কন্সুমার জেনারেটর
|
| 119 |
def consumer():
|
| 120 |
+
try:
|
| 121 |
+
while True:
|
| 122 |
+
try:
|
| 123 |
+
# সর্বোচ্চ ১৫ সেকেন্ড অপেক্ষা করবে, ফলে থ্রেড কখনো অনন্তকাল হ্যাং হবে না
|
| 124 |
+
chunk = q.get(timeout=15)
|
| 125 |
+
except queue.Empty:
|
| 126 |
+
break
|
| 127 |
+
if chunk is None:
|
| 128 |
+
break
|
| 129 |
+
yield chunk
|
| 130 |
+
except GeneratorExit:
|
| 131 |
+
# ইউজার যদি মাঝপথে ব্রাউজার ট্যাব কেটে দেয়, তবে কিউ খালি করে ব্যাকগ্রাউন্ড থ্রেড রিলিজ করা হবে
|
| 132 |
+
while not q.empty():
|
| 133 |
+
try: q.get_nowait()
|
| 134 |
+
except: break
|
| 135 |
|
| 136 |
return consumer()
|
| 137 |
|
|
|
|
| 1006 |
elif state.get("step") == "broadcast":
|
| 1007 |
await process_broadcast(client, message)
|
| 1008 |
|
| 1009 |
+
def run_flask(): app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)), threaded=True) # Multithreading explicitly enabled
|
| 1010 |
|
| 1011 |
async def main():
|
| 1012 |
await bot.start()
|