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
pppguest3962
V2EX  ›  Python

是否有子进/线程对主线程队列进行操作的方法?以及回调函数范围的问题。。。

  •  
  •   pppguest3962 · 2020-09-23 00:33:55 +08:00 · 1486 次点击
    这是一个创建于 1283 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python 是一知半解,属于急忙有车就上的认知,摸了一天才知道子进 /线程无法直接去操作主线程的队列,
    改变想法,从进 /线程里面回调"推"任务出来,在进 /线程结束后,由主线程对列表任务进行操刀处理加入到队列,
    才发现在回调函数里面似乎受限制了,如何破?

    # 最终结果列表,全局的味道
    GGlobalList = []
    
    def procSome(xx):
        return xxList
    
    def multiProcFun():
        BABTaskDict = {'TkNum': 32,
                       'TkString': 'test String'}
    
        def procFun(taskInfo):
            aDataList = []
            for i in range(1, 7)
                taskNumber = taskInfo.get('TkNum')
                taskString = taskInfo.get('TkString')
                somethingList = procSome(taskNumber, taskString)
                ...
                ...
                aDataList = aDataList + somethingList
            if aDataList:
                return aDataList
            else:
                return None
    
        with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
            def cb_fun(res):
                data = res.result()
                if data:
                    # global GGlobalList # 很显然这里就算声明了要用,也是不行的
                    # GGlobalList = GGlobalList + data   <--报:
                    # UnboundLocalError: local variable 'addtomtQueenList' referenced before assignment
                    # 错误
    
            to_do = []
            future = executor.submit(procFun, BABTaskDict).add_done_callback(cb_fun)
            to_do.append(future)
    
    if __name__ == '__main__':
        ltQueen = queue.LifoQueue()
        multiProcFun() 
        for i in GGlobalList:
            ltQueen.put(i)
            
        # 如果能在 procFun 里面直接操作 ltQueen 就好了...
    
    4 条回复    2020-09-23 11:16:00 +08:00
    formaxin
        1
    formaxin  
       2020-09-23 08:09:09 +08:00 via Android
    线程和进程都有队列吧
    multiprocessing.queuq
    xiaolinjia
        2
    xiaolinjia  
       2020-09-23 08:57:50 +08:00
    子线程无法操作主线程的队列?子进程无法操作主进程的队列?就扯淡。本来队列就是线程和线程通信,进程和进程通信的一种方法。
    只是,多线程用的队列是 queue 模块下的。多进程用的队列是 multiprocessing 模块下的 Queue 。
    然后你那 queue.LifoQueue 是用于多线程的。
    chaoshui
        3
    chaoshui  
       2020-09-23 11:04:41 +08:00
    回复不支持 markdown 啊,有点恶心
    不是很明白你要做什么, 是要在回调中拿到返回值修改全局变量吗
    chaoshui
        4
    chaoshui  
       2020-09-23 11:16:00 +08:00
    变量命名不规范, 改了一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5395 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:50 · PVG 15:50 · LAX 00:50 · JFK 03:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.