推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
617450941
V2EX  ›  Python

python 已经启动的线程 怎么往里面传参数

  •  
  •   617450941 · Nov 8, 2015 · 5919 views
    This topic created in 3982 days ago, the information mentioned may be changed or developed.
    一个类里面启动用线程启动一个函数 然后外部调用这个类中的另一个函数 这个函数再把参数传入这个线程中的函数 该怎么写 我直接调用这个启动中的线程 发现不能传
    11 replies  •  2015-11-09 16:50:56 +08:00
    617450941
        1
    617450941  
    OP
       Nov 8, 2015
    提示这个 线程启动后 再次传参数 就提示这个 db thread except:
    SYP
        2
    SYP  
       Nov 8, 2015 via Android
    线程间通信。
    617450941
        3
    617450941  
    OP
       Nov 8, 2015
    @SYP 这个这么做线程间通信 比如两个线程 1 2 启动了 现在 2 线程要传一个参数给线程一里的一个函数 该怎么做
    SYP
        4
    SYP  
       Nov 8, 2015
    @617450941 可以用全局变量加锁,这个最简单,消息和 socket 也可以。
    BiggerLonger
        5
    BiggerLonger  
       Nov 8, 2015
    用队列也可以吧
    limbo0
        6
    limbo0  
       Nov 8, 2015
    用 queue, 线程安全的, 比较方便
    gancl
        7
    gancl  
       Nov 8, 2015
    ```
    def startTheadMsg(self,msg):
    #for i in range(1):
    try:
    t = Thread(target=msgGet, args=(self,msg))
    t.start()
    except Exception as inst:
    log.info( str(type(inst) )+"&&&"+str(inst.args)+"###"+str(msg) )
    pass
    ```
    zaishanfeng
        8
    zaishanfeng  
       Nov 8, 2015
    公共区域一个放一个取就 ok 了 哦 对了 别搞乱套了
    KotiyaSanae
        9
    KotiyaSanae  
       Nov 8, 2015
    https://gist.github.com/SeavantUUz/68ee6cb1c59822c7d5e3
    其实没什么不行的,我这里用了队列,只是好演示
    owwlo
        10
    owwlo  
       Nov 9, 2015
    @617450941 可以共享下你需要应用的那部分的代码吗
    wizardoz
        11
    wizardoz  
       Nov 9, 2015
    python 的 queue 是线程安全的,用 queue 传递就好。
    线程 A 要传递数据的时候直接 put 到 queue 里面。
    线程 B 跑累了就停下来看看 queue 里面有没有数据。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   851 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 74ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    ♥ Do have faith in what you're doing.