V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2015-11-08 12:35:20 +08:00 · 4702 次点击
    这是一个创建于 3089 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个类里面启动用线程启动一个函数 然后外部调用这个类中的另一个函数 这个函数再把参数传入这个线程中的函数 该怎么写 我直接调用这个启动中的线程 发现不能传
    11 条回复    2015-11-09 16:50:56 +08:00
    617450941
        1
    617450941  
    OP
       2015-11-08 12:44:01 +08:00
    提示这个 线程启动后 再次传参数 就提示这个 db thread except:
    SYP
        2
    SYP  
       2015-11-08 12:56:15 +08:00 via Android
    线程间通信。
    617450941
        3
    617450941  
    OP
       2015-11-08 12:58:27 +08:00
    @SYP 这个这么做线程间通信 比如两个线程 1 2 启动了 现在 2 线程要传一个参数给线程一里的一个函数 该怎么做
    SYP
        4
    SYP  
       2015-11-08 13:05:49 +08:00
    @617450941 可以用全局变量加锁,这个最简单,消息和 socket 也可以。
    BiggerLonger
        5
    BiggerLonger  
       2015-11-08 15:46:57 +08:00
    用队列也可以吧
    limbo0
        6
    limbo0  
       2015-11-08 17:16:53 +08:00
    用 queue, 线程安全的, 比较方便
    gancl
        7
    gancl  
       2015-11-08 17:27:56 +08:00
    ```
    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  
       2015-11-08 17:30:53 +08:00
    公共区域一个放一个取就 ok 了 哦 对了 别搞乱套了
    KotiyaSanae
        9
    KotiyaSanae  
       2015-11-08 19:48:18 +08:00
    https://gist.github.com/SeavantUUz/68ee6cb1c59822c7d5e3
    其实没什么不行的,我这里用了队列,只是好演示
    owwlo
        10
    owwlo  
       2015-11-09 14:15:52 +08:00
    @617450941 可以共享下你需要应用的那部分的代码吗
    wizardoz
        11
    wizardoz  
       2015-11-09 16:50:56 +08:00
    python 的 queue 是线程安全的,用 queue 传递就好。
    线程 A 要传递数据的时候直接 put 到 queue 里面。
    线程 B 跑累了就停下来看看 queue 里面有没有数据。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3537 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:11 · PVG 19:11 · LAX 04:11 · JFK 07:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.