Python Forum
threading across imports - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: threading across imports (/thread-30820.html)



threading across imports - Nickd12 - Nov-08-2020

so the problem i am having is that im trying to get my text from a main .py file and with txt being at the end of this line here t = threading.Thread(target=reminder(txt))
it does not work properly it should just be target = reminder with that it works fine only if i get my text from this file basically what im asking is how can i get text = input(cmd: ) from my main file with out having circular import issues and get the threading working properly also so in short i need to avoid having the txt argument in the reminder function but still being able to pass the text from my main.py file through the function so it knows what it is looking for

import time
import datetime
import re
import threading


def find_time_in_text(txt):
    pattern = r"(\d+)(?::(\d+))?\s*([ap]\.m\.)"

    m = re.search(pattern, txt)
    if m:
        hour, minute, am_pm = m.groups()
        time_ = f"{hour}:{minute or 0} {am_pm}"
        return time_
    else:
        print("Couldn't find a time in the right format")


def reminder(txt):
    if "a.m." in txt or "p.m." in txt:
        get_time = find_time_in_text(txt)
        # Getting the time
        time_to_list = get_time.split()
        # this splits time to a list
        time_to_split = time_to_list[0]
        # This gets hour and minute
        split_time_ = time_to_split.split(":")
        # This splits hour and minutes
        hour = split_time_[0]
        minute = split_time_[1]
        am_pm_ = time_to_list[1]
        alarm_hour = int(hour)
        alarm_minutes = int(minute)
        am_pm = str(am_pm_)
        print(f"A reminder is set for: {alarm_hour}:{alarm_minutes} {am_pm}")
        if am_pm == 'p.m.':
            alarm_hour += 12
        elif alarm_hour == 12 and am_pm == 'a.m.':
            alarm_hour -= 12
        else:
            pass
        while True:
            if alarm_hour == datetime.datetime.now().hour and alarm_minutes == datetime.datetime.now().minute:
                print("\nIt's the time!")
                print("alarm")
                break

    if "minute" in txt or "minutes" in txt:
        for s in txt.split():
            if s.isdigit():
                number = int(s)
                reminder2 = number * 60
                print(f"A reminder is set for {number} minutes from now.")
                time.sleep(reminder2)
                print("reminder!")
                return

    elif "second" in txt or "seconds" in txt:
        for s in txt.split():
            if s.isdigit():
                number = int(s)
                reminder2 = number
                print(f"A reminder is set for {number} seconds from now.")
                time.sleep(reminder2)
                print("reminder!")
                return

    else:
        return


def thred(txt):
    threads = []
    for _ in range(1):
        t = threading.Thread(target=reminder(txt))
        t.start()
        threads.append(t)
    for thread in threads:
        thread.join(0)



RE: threading across imports - deanhystad - Nov-08-2020

t = threading.Thread(target=reminder(txt))
This is not how you pass arguments to a thread, This calls reminder(txt) and then tries to launch a tread using the return value which is None. In effect you are calling
t = threading.Thread(target=None)
You want to do this:
t = threading.Thread(target=reminder, args=(name,))
What does this have to do with imports?


RE: threading across imports - Nickd12 - Nov-09-2020

ahh that worked thank you and because I have a while true loop in a different .py file where it gets an input information from. I didn't know you could add args to threading and it worked perfectly thank you so much!