#πŸ”’ Multi-threading

9 messages Β· Page 1 of 1 (latest)

mental jolt
#

Hello guys, I was wondering if it is possible to execute the 5 other python file simultaneously/concurrently with our main python file waiting for all the 5 file to be executed first then continues with its execution... the problem that arise is that when I try to do multi-threading, our main thread continues to execute so our main python file finish executing without waiting for the other python file to terminate.

# main.py

# Execute each script
print("\nFetching information from lemauricien.mu\n")
exec(open("A_Reading_File.py").read())
print("\nFetching information from Actu.mu\n")
exec(open("B_Reading_File.py").read())
print("\nFetching information from defimedia.info\n")
exec(open("C_Reading_File.py").read())
print("\nFetching information from lexpress.mu\n")
exec(open("D_Reading_File.py").read())
print("\nFetching information from ionnews.mu\n")
exec(open("E_Reading_File.py").read())

# Merging information from multiple files into Results.txt

# List of input files
input_files = [
    "leMauricien_info.txt",
    "ActuFile.txt",
    "DefimediaFile.txt",
    "expressFile.txt",
    "IonNewsFile.txt"
]

# Output file
output_file = "Results.txt"

# Open the output file in write mode
with open(output_file, "w", encoding="utf-8") as outfile:
    # Iterate through each input file
    for filename in input_files:
        # Open each input file in read mode
        with open(filename, "r", encoding="utf-8") as infile:
            # Read the content of the input file
            file_content = infile.read()
            # Write the content to the output file
            outfile.write(file_content)
            # Add a separator between the content of different files
            outfile.write("\n--- End of {} ---\n\n".format(filename))

print("Merge complete. Results saved in", output_file)
uncut mossBOT
#

@mental jolt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thin pumice
#

u would basically thread all of them and then join waiting for each thread to finish

#

!d threading.Thread.join

uncut mossBOT
#

join(timeout=None)```
Wait until the thread terminates. This blocks the calling thread until the thread whose [`join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

When the *timeout* argument is present and not `None`, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As [`join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) always returns `None`, you must call [`is_alive()`](https://docs.python.org/3/library/threading.html#threading.Thread.is_alive) after [`join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) to decide whether a timeout happened – if the thread is still alive, the [`join()`](https://docs.python.org/3/library/threading.html#threading.Thread.join) call timed out.

When the *timeout* argument is not present or `None`, the operation will block until the thread terminates.

A thread can be joined many times.
thin pumice
#

once all the joins are finished u can resume execution of the main.py code

bold ether
#

use import over exec(open().read())
preferably create a function that will return the content as a string instead of having the file always write to a fixed output, e.g. ```py

runner.py

def run():
# move all of your code into here
return "hello world"

if name == "main":
data = run()
with open("output.txt", 'w') as file:
file.write(data)

main.py

from concurrent import futures

from runner import run
from other_runner import other_run

results = [run(), other_run()]

pool = futures.ThreadPoolExecutor()
calls = [pool.submit(func) for func in (run, other_run)]
futures.wait(calls)

for call in calls:
print(call.result())
...

uncut mossBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.