You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3报TypeError: 'module' object is not callable 修复Replit Discord机器人保活错误

错误原因

  • 报错根因是你直接将_thread模块对象作为可调用对象执行,_thread是Python内置的低层级线程模块,本身不是可调用的构造函数,需要调用模块内置的线程创建方法才能正常使用。

修正方案

方案1:继续使用_thread模块

修改keep_alive函数内的线程创建逻辑,调用_thread.start_new_thread方法即可,修改后的完整代码如下:

import flask
import _thread

app = flask.Flask('Keep Alive')

@app.route('/')
def home():
    return "I'm alive"

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():
    _thread.start_new_thread(run, ())

方案2(更推荐):替换为threading模块

threading是Python封装的高层级线程接口,易用性和兼容性更好,修改后的完整代码如下:

import flask
import threading

app = flask.Flask('Keep Alive')

@app.route('/')
def home():
    return "I'm alive"

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():
    t = threading.Thread(target=run)
    t.start()

额外注意事项

  • 修正代码后,需要在你的Discord机器人主代码的启动逻辑前导入并调用keep_alive(),保活逻辑才会生效。
  • Replit免费版实例无外部请求的情况下1小时后会自动休眠,你可以搭配定时 ping 工具定期访问项目对应的web地址延长在线时间。

内容的提问来源于stack exchange,提问作者K.Arie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 20:36:06