#πŸ”’ A small question regarding mixing the threading and queue module

104 messages Β· Page 1 of 1 (latest)

cinder veldt
#

I need some help on transferring data between the main code and the Thread I started with the code i wrote. The function that runs on the thread is on a different file though. The data i want to transfer is only the Language value.

hard carbonBOT
#

@cinder veldt

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.

cinder veldt
#

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...

abstract gazelle
#

ask if there's anything unclear

#

by "in a different file" I assume you mean a different module, but still within the same process

cinder veldt
#

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

abstract gazelle
#

ok so what's unclear?

cinder veldt
abstract gazelle
#

that's not really a question

#

can you show me your code so far?

cinder veldt
#

I can't send the whole code in here, but i can send it on Pastebin (the Python one)

neon granite
#

!paste

hard carbonBOT
#
Pasting large amounts of code

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.

cinder veldt
#

Yeah, i have the old version still

abstract gazelle
#

the phrase "send language variable" sounds wrong

#

you don't "send variables" in Python

#

but seeing your code would probably shed light to this

cinder veldt
#

Yeah, it is somewhat of a Mess

neon granite
#

The second file has functions with language parameters. Did you mean you want to pass the language from the main file to those functions?

cinder veldt
#

Yeah, I don't know how to do that.

neon granite
#

timereader(settings.language, logg)?

#

Idk what logg is supposed to be though.

cinder veldt
neon granite
#

Oh, then just something like timereader(settings.language, True).

cinder veldt
#

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

cinder veldt
neon granite
#

Why is it being started with the wrong language in the first place? It doesn't look like setting.language ever changes.

cinder veldt
#

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

neon granite
neon granite
#

You should only need to restart the thread if some initialization required a specific language and that needed to be rerun

neon granite
# cinder veldt May I ask how?
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.

cinder veldt
neon granite
#

They're the same object.

cinder veldt
#

with the threading thread?

neon granite
cinder veldt
#

Okay, let's start from 1: The Thread starts with this variable and when changing the language, it only saves to the settings.json

neon granite
#

If you're already doing that before saving the JSON, the other thread will see that you changed setting.language.

cinder veldt
#

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

neon granite
#

You restarted the main thread?

cinder veldt
#
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.

neon granite
#

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.

cinder veldt
#

From the function init it starts and when i do the return thing it changed the Settings. Except the one thing

neon granite
#

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.

cinder veldt
#

I'm not that far that i know how to pass the right settings back yet.

neon granite
#

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.

cinder veldt
neon granite
#

Just add setting as a parameter and mutate it instead.

cinder veldt
#

okay i think the point didn't come across

#

The code is figuratively not in the Same file

neon granite
cinder veldt
#

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

cinder veldt
cinder veldt
#

Okay, Let's say it as that, I can try my best again and then if I have issues i'm back then

neon granite
#

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.

cinder veldt
#

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.

neon granite
#

That can be done using queues like you already have, or by just mutating an object given to the thread.

cinder veldt
#

Okay, How do i Mutate the object given to the thread?

neon granite
#

setting.language = 'New Language' is mutating setting if that wasn't clear.

cinder veldt
#

There is no class "setting" in the python file that has the title stuff

neon granite
#

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.

cinder veldt
#

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?

neon granite
cinder veldt
#

oh πŸ˜…

neon granite
#

Threads can access objects created in other threads.

cinder veldt
#

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

cinder veldt
#

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

neon granite
#

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.

cinder veldt
#

yup, it is done

#

!close

hard carbonBOT
#
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.