V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MrGba2z
V2EX  ›  问与答

请教下这样的 python script 如何改成多线程

  •  
  •   MrGba2z · 2015-10-23 23:56:36 +08:00 · 1212 次点击
    这是一个创建于 3120 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大概的代码是:

    def get_info(user):
        children = get_children(user)
        add_to_file(user, children)
        for i in children:
            get_info(i)
    

    这个程序的瓶颈在于get_child()需要连接远程数据库获取,花费不少时间。

    假设数据库不会拒绝同一时间的多次访问,如何优化这个脚本?(这应该用多线程比较合适吧)

    2 条回复    2015-10-24 00:05:55 +08:00
    emric
        1
    emric  
       2015-10-24 00:03:40 +08:00
    I/O 瓶颈应该用异步吧.
    gamexg
        2
    gamexg  
       2015-10-24 00:05:55 +08:00 via Android
    队列 线程池。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5242 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:38 · PVG 13:38 · LAX 22:38 · JFK 01:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.