Q:

Cant get the result when I use multi threading with Python

Im trying to make Instagram scrapper with one Python lib. It goes well but its very slow. Im trying to speed it up by using multithreading but the other problem occurred.
This is the code without multithreading, it works good:
import threading
import instaloader
import time

L = instaloader.Instaloader() 

def func1(name):

    first = []
    posts = instaloader.Profile.from_username(L.context, name).get_posts()
    posts = list(posts)

    for p in posts[0:5]:
        first.append(p)

    return first

def func2(name):

    second = []
    posts = instaloader.Profile.from_username(L.context, name).get_posts()
    posts = list(posts)

    for p in posts[5:10]:
        second.append(p)

    return second

t = time.time()

print(func1('eminem'))
print(func2('eminem'))

print(time.time()-t)  # this is 47.43 seconds

But when I try to use multithreading, i see that time of execution of my code is much shorter, but I do not get the result, It does not work with 'return' statement. I need to use the return statement because this is only just a part of the code, so I cant use print.
This is the code with threads:
L = instaloader.Instaloader() 

def func1(name):

    first = []
    posts = instaloader.Profile.from_username(L.context, name).get_posts()
    posts = list(posts)

    for p in posts[0:5]:
        first.append(p)

    return first

def func2(name):

    second = []
    posts = instaloader.Profile.from_username(L.context, name).get_posts()
    posts = list(posts)

    for p in posts[5:10]:
        second.append(p)

    return second

t = time.time()

t1 = threading.Thread(target = func1, args=('eminem',))
t2 = threading.Thread(target = func2, args=('eminem',))

t1.start()
t2.start()

t1.join()
t2.join()

print(time.time()-t)  # this is 25.36 seconds

What am I doing wrong?

A:

The easiest way in your case is to pass a shared data structure with distinguished keys to accumulate results from different functions:
Instead of using local lists first = [] ; second = [] - append result to shared structure like:
def func1(name, results):
    ...
    for p in posts[0:5]:
        results['func1'].append(p)

The same for func2 function.

results = {'func1': [], 'func2': []}
t1 = threading.Thread(target = func1, args=('eminem', results))
t2 = threading.Thread(target = func2, args=('eminem', results))

t1.start()
t2.start()

t1.join()
t2.join()

print(results) 

Another option is using concurrent.futures.Executor.submit approach.

