Python Forum
Python multiprocessing Pool apply async wait for process to complete
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python multiprocessing Pool apply async wait for process to complete
#1
I have coded a simple program of run through the list, but apply_sync isn't waiting until the list is complete, here is the code

import itertools
import multiprocessing
from multiprocessing import Pool
import time
from tkinter import *
from tkinter import ttk
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os
from tkinter import messagebox


x= ['annah', 'reena', 'jermy', 'sunny', 'chinni']


def next_name(name):
    options = Options()
    options.binary_location = r'drivers/Browser/chrome.exe'
    driver = webdriver.Chrome(executable_path=r'drivers/chromedriver.exe', options=options)
    driver.get('http://google.com')
    time.sleep(2)
    driver.find_element(By.XPATH, '//input[@name="q"]').send_keys(name)
    time.sleep(2)
    driver.find_element(By.XPATH, '//input[@name="q"]').send_keys(Keys.ENTER)
    time.sleep(3)
    driver.close()

def start():
    global y
    y = Pool(processes=2)
    for usr in itertools.zip_longest(x):
        y.apply_async(next_name, args=(usr, ))
    #messagebox has to popup when list is completed
    messagebox.showinfo("Sample Program", "List Completed")

def stop():
    y.terminate()
    os.system("taskkill /F /IM chromedriver.exe /T")
root = Tk()
startbtn = ttk.Button(root, text="start", command=start)
startbtn.pack()
stopbtn = ttk.Button(root, text="stop", command=stop)
stopbtn.pack()
root.geometry('300x300')
if __name__ == '__main__':
    multiprocessing.freeze_support()
    root.mainloop()
I just want the program to show message box when list was completed.

Any help would be appreciated.

Thank you
Reply
#2
If you call the function directly the program will wait and draw the message block when the processes are done. By using apply_async() you are telling python to not wait for completion of the tasks. apply_async() returns an AsyncResult object. This can be used to wait for the process to complete. You can also specify a callback function that is called when the task completes.

You should not block as this will make the user interface unresponsive. I think using the callback is a better idea.

apply_async() is not a good choice for what you are doing. I would go with map_async()

https://docs.python.org/3/library/multiprocessing.html
sunny9495 likes this post
Reply
#3
Thanks for the help deanhystad, i modify the code with map_async, it works fine

def mycallback(x):
    messagebox.showinfo("Sample Program", "Completed")

def start():
    global y
    pool = Pool(processes=2)
    pool.map_async(next_name, x, callback=mycallback)
(Mar-26-2022, 06:48 AM)deanhystad Wrote: If you call the function directly the program will wait and draw the message block when the processes are done. By using apply_async() you are telling python to not wait for completion of the tasks. apply_async() returns an AsyncResult object. This can be used to wait for the process to complete. You can also specify a callback function that is called when the task completes.

You should not block as this will make the user interface unresponsive. I think using the callback is a better idea.

apply_async() is not a good choice for what you are doing. I would go with map_async()

https://docs.python.org/3/library/multiprocessing.html
Reply
#4
How we can pass multiple lists in map_async, example

x= ['annah', 'reena', 'jermy', 'sunny', 'chinni']
proxies = ["ip:port:username:password", "ip:port:username:password"]


I want to use the proxies randomly for every launch
Reply
#5
Still looking for solution on how to add multiple args to map_async
Reply
#6
Lots of choices. I'm sure one fits you needs.

https://docs.python.org/3/library/multip...1a25acdf81

Maybe one of the starmap functions.
sunny9495 likes this post
Reply
#7
Thanks alot @deanhystad i found the solution
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is this a multiprocessing bug in Python - or am I doing something wrong? haimat 1 1,200 Oct-18-2023, 06:07 AM
Last Post: absalom1
  queue for async function python telegram.ext noctious 0 1,608 Jun-11-2023, 02:58 PM
Last Post: noctious
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 834 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Question about Creating an Automated Process in Python Supratik1234 0 753 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  python multiprocessing help -- to extract 10 sql table into csv mg24 3 1,406 Nov-20-2022, 11:50 PM
Last Post: mg24
  Multiprocessing Pool Multiple Instances How to Kill by Pool ID sunny9495 0 767 Nov-16-2022, 05:57 AM
Last Post: sunny9495
  python multiprocessing to download sql table mg24 5 1,496 Oct-31-2022, 03:53 PM
Last Post: Larz60+
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,056 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Wait til a date and time KatManDEW 2 1,439 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  wait for the first of these events Skaperen 4 1,975 Mar-07-2022, 08:46 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020