-1

I have used python to make a program that can get the name of similar Movies. I have used threading to make sure that the functions run parallel to prevent time wastage.

import threading
import requests
from bs4 import BeautifulSoup
url = "https://www.movie-map.com/twilight.html"
url2 = "https://www.movie-map.com/Interstellar.html"
x = ''
word_list = []
def spider(url):
    word_list = []
    try:
        source_code = requests.get(url)
        plain_text =  source_code.text
        soup = BeautifulSoup(plain_text, features="lxml")
        for link in soup.find('div', attrs = {'id':'gnodMap'}):
            title = link.string
            word_list.append(title)
    except:
        pass
    x = word_list
    return word_list

t1 = threading.Thread(target=spider, args=[url])
t2 = threading.Thread(target=spider, args=[url2])
t1.start()
t2.start()

How should I get the returned value from the functions?

1 Answers1

0

Probably the simplest way is through the queue.Queue class.

import queue
import threading


q = queue.Queue()
t = threading.Thread(target=lambda: q.put('Hello!'))
t.start()

print(q.get(timeout=5))  # "Hello!"

You can also use a threading.Lock to synchronize shared access to variables like this:

import time


class Links(object):

    def __init__(self):
        self._lock = threading.Lock()
        self._links = []

    @property
    def links(self):
        self._lock.acquire()
        links = list(self._links)
        self._lock.release()
        return links

    def add(self, link):
        self._lock.acquire()
        self._links.append(link)
        self._lock.release()


l = Links()


def target():
    time.sleep(2)
    l.add('link')


t = threading.Thread(target=target)
t.start()

print(l.links) # []
time.sleep(2)
print(l.links) # ['link']
kingkupps
  • 3,284
  • 2
  • 16
  • 28