def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(run_browser_session, i + 1) for i in range(4)]
if name == “main”:
main()
each process should run n number of threads. threads should execute after previous thread has finished. also rename the trace.zip so that it contains process number and thread number
First, to run threads sequentially within each process and to rename the trace file to include both the process number and thread number, you need to make some adjustments to your code.
The function run_browser_session doesn't directly allow for running multiple threads within it as it stands. Since you're using Playwright, which is inherently synchronous in its synchronous API variant, you'll manage the sequential execution of what you're calling "threads" (which, within the context of your application, will be sequential operations rather than concurrent threads) via direct function calls. To achieve renaming with process and thread numbers, you can pass an additional parameter to your function for the thread number and construct the filename dynamically based on these values.
Here's an adjusted version of your code that demonstrates these modifications. For brevity and focus on the requested changes, I will not include the entire body of run_browser_session as it remains unchanged apart from the addition of handling the thread number and renaming the trace file accordingly:
import concurrent.futures
import time
# Assuming other necessary imports and setup are unchanged
def run_browser_session(process_number, thread_number) -> bool:
with sync_playwright() as p:
# Launch browser and perform actions as before...
# The trace filename now includes both process and thread number
trace_filename = f"trace_P{process_number}_T{thread_number}.zip"
# Perform automation as before with Playwright...
# Replace context.tracing.stop() call with the new filename
context.tracing.stop(path=trace_filename)
# Context and browser closure code as before...
# Timer end and print statement as before
return True
def main():
# Number of iterations (threads) you want each process to run
num_threads_per_process = 3
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
futures = []
for process_num in range(4):
for thread_num in range(num_threads_per_process):
# Submit a new future for each thread of each process
future = executor.submit(run_browser_session, process_num + 1, thread_num + 1)
futures.append(future)
# Remember to correct the check, it should be '__name__' not 'name'
if __name__ == "__main__":
main()
In this revised version, run_browser_session receives an additional argument (thread_number) which is used to generate the trace filename that includes both the process and thread identifiers.
The main function has been modified to create a total of num_threads_per_process * num_workers tasks, where each task is a separate call to run_browser_session with unique process and thread numbers. Adjust num_threads_per_process based on how many sequential "threads" (tasks) you want per process.
Important Note: Playwright operates within a single thread per process when using the sync API, hence "threads" in the context of your question are treated as sequential tasks within each process rather than traditional concurrent threads. If truly parallel, concurrent execution is desired, adjustments to the code and possibly the architecture (e.g., using async features or further subdividing tasks) would be necessary.