As of Python function to get the variable?

There is a function from which you want to pull one variable. return cannot be used because threading is used which does not return what the function returns. Need to get variable data from function read_sok as a global so that it can be treated.
def messageShow(message, alias):
 def read_sok():
 while 1:
 data = sor.recv(1024)
 data = data.decode("utf-8")
 data_list = data.split()
 if data_list[1] != alias:

 server = '', 9090 # the server information
 # Enter our alias
 sor = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 sor.bind((", 0)) # Set the socket as a client
 sor.sendto((alias + 'Connect to server').encode('utf-8'), server) # Notify the server about the connection
 potok = threading.Thread(target=read_sok)
 while 1:
 sor.sendto(('[' + alias + '] ' + message).encode('utf-8'), server)
April 4th 20 at 13:04
4 answers
April 4th 20 at 13:06
Turn use.
April 4th 20 at 13:08
Record the value of this variable in a global variable.
here's how to do just not worth it, especially with streams - payton_Willms68 commented on April 4th 20 at 13:11
@payton_Willms68, And why?

Although it is clear that if is a complex structure or a long line - the question of atomicity. Well, to add a lock change/read. - Kenneth_Waelc commented on April 4th 20 at 13:14
writes is not defined - Hollis_Dicki commented on April 4th 20 at 13:17
@Kenneth_Waelcit's harder to control the condition, plus it is more difficult to debug and manage.
for sharing data between threads threads is more than the right thing, queues, signals, and so on.
The use of global in the code is a sign of bad code - payton_Willms68 commented on April 4th 20 at 13:20
April 4th 20 at 13:10
April 4th 20 at 13:12
from threading import Thread

def foo(bar):
 return bar

class ThreadWithReturnValue(Thread):
 def __init__(self, group=None, target=None, name=None,
 args=(), kwargs={}, Verbose=None):
 Thread.__init__(self, group, target, name, args, kwargs)
 self._return = None
 def run(self):
 if self._target is not None:
 self._return = self._target(*self._args,
 def join(self, *args):
 Thread.join(self, *args)
 return self._return
twrv = ThreadWithReturnValue(target=foo, args=('Hello, world!'))

print(twrv.join()) # prints Hello, world!

Taken from here
You read that the challenge was to bring the variable and not print. - Hollis_Dicki commented on April 4th 20 at 13:15

Find more questions by tags Python