首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python APIv4:多线程和多线程同时进行

Python APIv4:多线程和多线程同时进行
EN

Stack Overflow用户
提问于 2019-03-31 00:32:02
回答 1查看 468关注 0票数 1

我可以让多线程和多线程独立使用google APIv4,但我不能让它们一起工作。

多重处理(这是可行的):

代码语言:javascript
复制
from multiprocessing import Pool
import threading
import pandas

class B:
    def __init__(self):
        self.core = 10
        self.b()

    def b(self):
        p = Pool(self.core)
        p.map(multicore, range(10))

def multicore(*args):
    thread = 0
    if thread == 1:
        thread_list = []
        for i in range(10):
            thread = threading.Thread(target=output_function, args=(i,))
            thread_list.append(thread)
            thread.start()
    else:
        output_function(*args)

def output_function(*args):
    x = args[0]

    print(x * x)
    g.build_service()

    g.export_df(g.test_API_key, ['output!A' + str(x + 1)], [pandas.DataFrame([[x * x]])], 'n')

多线程(这也有效):

代码语言:javascript
复制
def just_threading():
    thread = 1
    if thread == 1:
        thread_list = []
        for i in range(10):
            thread = threading.Thread(target=output_function, args=(i,))
            thread_list.append(thread)
            thread.start()

def output_function(*args):
    x = args[0]

    print(x * x)
    g.build_service()

    g.export_df(g.test_API_key, ['output!A' + str(x + 1)], [pandas.DataFrame([[x * x]])], 'n')

但是,当我通过将thread = 1设置为第一个示例将它们组合在一起时,我绝对得不到google的输出(print(x*x)仍然有效)。

有趣的是,如果我去掉g.build_service(),它将输出几行代码,直到遇到这里概述的线程安全问题:safety和error ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2273)。因此,重建服务固然重要,但我没有看到输出到工作表!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-02 21:38:46

您需要等待线程完成它们的工作,否则主线程将在它们完成之前退出。

启动线程后,运行:

代码语言:javascript
复制
for t in thread_list:
    t.join()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55436851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档