Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
threading across imports
#1
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)
Reply
#2
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?
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,718 May-03-2023, 08:22 AM
Last Post: billykid999
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,550 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,076 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Imports in my first package cuppajoeman 1 1,912 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 2,481 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,767 Mar-17-2021, 11:57 AM
Last Post: cff
  refreshing imports seandepagnier 4 2,674 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,374 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  absolute imports between folders mikisDW 0 1,505 Aug-05-2020, 12:26 PM
Last Post: mikisDW

Forum Jump:

User Panel Messages

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