简体   繁体   中英

Python Google Sheets APIv4: Multiprocessing and Multithreading at the same time

I can get multiprocessing and multithreading to work with google sheets APIv4 independently, but I can't get them to work together.

Multiprocessing (this works):

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')

Multithreading (this also works):

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')

But when I combine them by setting thread = 1 for the first example, I get absolutely no output to google sheets ( print(x*x) still works).

What's funny is that if I take away g.build_service() , it will output a few lines until it runs into Thread safety issues outlined here: https://developers.google.com/api-client-library/python/guide/thread_safety with error ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2273) . So rebuilding the service does matter, but I see no output to sheets!

You need to wait for the threads to finish their work, otherwise the main thread will exit before they complete.

After you've launched the threads, run:

for t in thread_list:
    t.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM