Basic threading with one background worker and other thread
<p>I'm very much a beginner in both python and programming. Trying to get multithreading work but haven't managed so far. Grateful for any help or tips.</p>

<pre><code>from threading import Thread
import time
import requests

class crawler:

    def get_urls(self):
        while True:
            #r = self.s.get('http:\\someurl')
            time.sleep(1)
            print 'Thread 1'

    def thread_test(self):
        while True:
            print 'Thread 2'
            time.sleep(1)

crawl = crawler()

if __name__ == '__main__':
    Thread(target=crawl.get_urls()).start()
    Thread(target=crawl.thread_test()).start()
</code></pre>

<p>It has been a while since i have done thread programming in python but I remembered that you will have to call <code>.join()</code> on each thread or else the main thread will exit before your spawn thread get a chance to execute.</p>

<pre><code>T1 = Thread(target=crawl.get_urls()).start()
T2 = Thread(target=crawl.thread_test()).start()
T1.join()
T2.join()
</code></pre>

<p>should do the trick</p>

<p><code>edit</code> I just looked into python <code>threading</code> library the issue is that <code>target</code> is expecting a callable object. when you call <code>target=crawl.get_urls()</code> the method was being evaluated rather than passing the method. </p>

<pre><code>from threading import Thread
</code></pre>

<p>import time
import requests</p>

<p>class crawler:</p>

<pre><code>def get_urls(self):
    while True:
        #r = self.s.get('http:\\someurl')
        time.sleep(1)
        print 'Thread 1'

def thread_test(self):
    while True:
        print 'Thread 2'
        time.sleep(1)

crawl = crawler()

if __name__ == '__main__':
    Thread(target=crawl.get_urls).start()
    Thread(target=crawl.thread_test).start()
</code></pre>

<p>ref: <a href="https://docs.python.org/3/library/threading.html#threading.Thread" rel="nofollow">https://docs.python.org/3/library/threading.html#threading.Thread</a></p>

python multithreading