Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Help with Threads
#3
You should not use global is terrible in almost all cases,and using threading dos not make it better.
Just to show some basic stuff with threading that may help.
from threading import Thread

def func1():
    print(f'Working')

def func2():
    print("Working")

if __name__ == '__main__':
    Thread(target=func1).start()
    Thread(target=func2).start()
If want argument to the function is done like(no value from global namespace) this with threading.
from threading import Thread
from time import sleep

def func1(name):
    sleep(5)
    print(f'Working {name}')

def func2():
    print("Working")

if __name__ == '__main__':
    name = 'Kent'
    Thread(target=func1, args=(name,)).start()
    Thread(target=func2).start()
See that working print immediately.
Without threading will now sleep block.
from threading import Thread
from time import sleep

def func1(name):
    sleep(5)
    print(f'Working {name}')

def func2():
    print("Working")

if __name__ == '__main__':
    name = 'Kent'
    func1(name)
    func2()
Reply


Messages In This Thread
[SOLVED] Help with Threads - by michaelserra - Jul-22-2021, 04:34 PM
RE: Help with Threads - by deanhystad - Jul-22-2021, 04:43 PM
RE: Help with Threads - by michaelserra - Aug-01-2021, 02:21 PM
RE: Help with Threads - by snippsat - Jul-22-2021, 05:46 PM

Forum Jump:

User Panel Messages

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