Skip to content

Instantly share code, notes, and snippets.

@bnchdrff
Created March 5, 2017 17:45
Show Gist options
  • Save bnchdrff/a7e1ed3dbf3b57a3f54ac641b8bd0017 to your computer and use it in GitHub Desktop.
Save bnchdrff/a7e1ed3dbf3b57a3f54ac641b8bd0017 to your computer and use it in GitHub Desktop.
v simple threads
import os
import threading
import urllib2
class ThreadHandler(threading.Thread):
def __init__(self, blocking_function):
threading.Thread.__init__(self)
self.runnable = blocking_function
def run(self):
self.runnable()
def get_10_sec():
urllib2.urlopen('https://httpbin.org/delay/10').read()
print 'get_10_sec done'
def get_5_sec():
urllib2.urlopen('https://httpbin.org/delay/5').read()
print 'get_5_sec done'
def run_the_thready():
print 'thread1 = ThreadHandler(get_10_sec)'
thread1 = ThreadHandler(get_10_sec)
print 'thread1.start()'
thread1.start()
print 'thread2 = ThreadHandler(get_5_sec)'
thread2 = ThreadHandler(get_5_sec)
print 'thread2.start()'
thread2.start()
print 'thread1.join()'
thread1.join()
print 'thread2.join()'
thread2.join()
print 'end of run_the_thready'
print 'run_the_thready()'
run_the_thready()
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment