我可以让多线程和多线程独立使用google APIv4,但我不能让它们一起工作。
多重处理(这是可行的):
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')多线程(这也有效):
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)。因此,重建服务固然重要,但我没有看到输出到工作表!
发布于 2019-04-02 21:38:46
您需要等待线程完成它们的工作,否则主线程将在它们完成之前退出。
启动线程后,运行:
for t in thread_list:
t.join()https://stackoverflow.com/questions/55436851
复制相似问题