#π A small question regarding mixing the threading and queue module
104 messages Β· Page 1 of 1 (latest)
@cinder veldt
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.
one of the examples i found:```py
from queue import Queue
from threading import Thread
A thread that produces data
def producer(out_q):
while True:
# Produce some data
...
out_q.put(data)
A thread that consumes data
def consumer(in_q):
while True:
# Get some data
data = in_q.get()
# Process the data
...
Create the shared queue and launch both threads
q = Queue()
t1 = Thread(target = consumer, args =(q, ))
t2 = Thread(target = producer, args =(q, ))
t1.start()
t2.start() ```
But there are parts that don't really make sense to me
And Examples wise, i don't even know how to get started...
ask if there's anything unclear
by "in a different file" I assume you mean a different module, but still within the same process
thanks, I mean it as such:
I have a Main code that runs and then the Main code runs a function in a thread that is in a Different File
ok so what's unclear?
I wanna send the language variable upon updating the language variable (Which the title time update thread is unaware of)
I can't send the whole code in here, but i can send it on Pastebin (the Python one)
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
Yeah, i have the old version still
the phrase "send language variable" sounds wrong
you don't "send variables" in Python
but seeing your code would probably shed light to this
That is what I mean on that the language variable thing:
Main file: https://paste.pythondiscord.com/NAIQ
The file that updates the Titlebar: https://paste.pythondiscord.com/3RIA
Yeah, it is somewhat of a Mess
The second file has functions with language parameters. Did you mean you want to pass the language from the main file to those functions?
Yeah, I don't know how to do that.
That is the file that is for the logging that i used: https://paste.pythondiscord.com/QD4Q
Oh, then just something like timereader(settings.language, True).
Yeah, it can be manually set to true or false
I think a refactoring is needed that the stuff can be done
I mean easily
Would you agree that on the refactoring to move that file (https://paste.pythondiscord.com/3RIA) to the Main file is gonna be a good choice?
Is stopping the thread and then starting the thread with the correct language variable is a good idea?
Why is it being started with the wrong language in the first place? It doesn't look like setting.language ever changes.
Okay, I change the Language and it stays for now on the Initial Language
All I mean is that if I change it mid runtime, I want that the thread also get's the new language variable
Or just pass settings directly to title_time and read setting.language right from the thread function.
May I ask how?
You should only need to restart the thread if some initialization required a specific language and that needed to be rerun
def title_time(setting, system, stop_event):
. . .
if setting.language=="de":
. . .
titletime=Thread(target=title_time, args=(setting, SysInf.system, stop_event, ))
Just pass it as an argument.
Yeah, I pass that argument on startup, But I made that the code can mid runtime change the language
Which is Basically in here: https://paste.pythondiscord.com/SO4A
If you mutate setting in the main thread, you'll see changes in the other thread.
They're the same object.
with the threading thread?
I'm not sure what you mean.
Okay, let's start from 1: The Thread starts with this variable and when changing the language, it only saves to the settings.json
When you change the language and save it to the file, you would also just do setting.language = 'New Language'
If you're already doing that before saving the JSON, the other thread will see that you changed setting.language.
Yeah, the way i handled it was until now is that i just restarted the main thread to load the settings until now, Which didn't affect the title thread
You restarted the main thread?
elif command=="language":
change_settings(settings="lang", prom=setting.prompt, language=setting.language, logging=setting.logg)
return```
a more redable version
Yeah, My code is not the best.
I don't see how that's restarting the thread. Afaik, there is no way to "restart the main thread". That's just reloading your program.
okay, How i handle the main thread is in there: https://paste.pythondiscord.com/NAIQ
From the function init it starts and when i do the return thing it changed the Settings. Except the one thing
change_settings is changing a separate variables object, which is unrelated to setting.
Oh, you're saving the JSON then reading it in that function.
Yeah...
I'm not that far that i know how to pass the right settings back yet.
What's the point of variables? Why not just give setting to change_settings and have it change setting directly?
Or house setting in that file and read the setting object from that file when you need to.
I didn't yet know how, The way I got it working was like the code right now...
def change_settings(setting, **kwargs):
. . .
setting.language=settings_file.get("language")
Just add setting as a parameter and mutate it instead.
okay i think the point didn't come across
The Main code is TextConverter.py and the Settings are in Settings.py
The code is figuratively not in the Same file
They don't need to be in the same file. If you pass setting from one file to a function in another file, the other file can mutate the object.
I do pass on startup the variable but then nothing else is done upon start of the Thread
And i just got an idea that I could code myself a fix for the Stupid Meta Link App
Oh, you mean like reading the file every so often?
Okay, Let's say it as that, I can try my best again and then if I have issues i'm back then
I just don't know what you're asking. Your original question was just "do I need to reload the thread to change setting", and it seemed to keep changing from there.
Yeah, It wasn't as clear as i hoped to explain
I want to send data to the thread I started with the module Threading.
That can be done using queues like you already have, or by just mutating an object given to the thread.
Okay, How do i Mutate the object given to the thread?
And then setting.language = 'New Language' when you want to change it.
setting.language = 'New Language' is mutating setting if that wasn't clear.
There is no class "setting" in the python file that has the title stuff
change_settings?
If you mean the file that has the thread function, that's why I show setting as an parameter of title_time in the comment I linked to.
okay, hold on
So if i give the thread that and the variable changes from the main part, that means that it changes for the thread too?
Yes. They're the same object,
oh π
Threads can access objects created in other threads.
Yeah, i can look on that in my Spaghetti code (Yes, I consider it spaghetti code)
First things first: i need to remove redundant stuff from the main file calling the function
nvm, i went to the wrong file
And since it is virtually not possible that the system it runs on mid running the code, i rule that out for now
Yeah.
thanks a lot for showing that π
Now I also know how i can handle themes on somewhere else π
Well, Hereby I would consider the Issue solved π
I'm not sure if i should close the thread
You're welcome.
And you might as well !close it if the main question is done. You can always create a new post later if you have a new question.
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.