Hey I am trying to make a automatic checkout bot, for now I made it so it can launch multiple undetected chrome instances :
driver = [(uc.Chrome(use_subprocess=True ,options=options1)),(uc.Chrome(use_subprocess=True, options=options2)),(uc.Chrome(use_subprocess=True, options=options3))]
and it runs the code like that :
def Main1():
i = 0
login(i)
checkout(i)
def Main2():
i = 1
login(i)
checkout(i)
def Main3():
i = 2
login(i)
checkout(i)
p1 = threading.Thread(target=Main1, args=())
p1.start()
p2 = threading.Thread(target=Main2, args=())
p2.start()
p3 = threading.Thread(target=Main3, args=())
p3.start()
i
stands for driver and account details index.
What I wanted to ask is
- how can I make this code more efficient? copying and pasting over and over doesn't seem like the most efficient thing
- if I want to run x amount of instances, where all instances have different account details, and choose the amount if instances by inputting the value, how can I do it without manually creating a new function and adding another item to the driver list?
CodePudding user response:
Create a threads list, run them with loop.
def instance(n):
return [uc.Chrome(use_subprocess=True,options=option) for m in range(0, n)]
def Main(i):
login(i)
checkout(i)
threads = [threading.Thread(target=Main, args=(i,)) for i in instance(int(input('num of instances: ')))]
for t in threads:
t.start()
CodePudding user response:
I have been able to the question using the following code :
buynumber = int(input('Buy number : '))
threadlist = []
driver = []
option = []
for i in range(0, buynumber):
threadlist.append(i)
threads = [threading.Thread(target=Main, args=(i,)) for i in threadlist]
for i in range(0, buynumber):
option.append(Options())
option[i].headless = True
option[i].add_argument("--disable-gpu")
option[i].add_argument("--incognito")
option[i].add_argument("--headless")
for i in range(0, buynumber):
driver.insert(i, uc.Chrome(use_subprocess=True,options=option[i]))
for t in threads:
t.start()
for t in threads:
t.join()
thanks to @Shuo for the help here