👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I have a script and I want one function to run at the same time as the other.
The example code I have looked at:
import threading
def MyThread (threading.thread):
# doing something........
def MyThread2 (threading.thread):
# doing something........
MyThread().start()
MyThread2().start()
I am having trouble getting this working. I would prefer to get this going using a threaded function rather than a class.
This is the working script:
from threading import Thread
class myClass():
def help(self):
os.system("./ssh.py")
def nope(self):
a = [1,2,3,4,5,6,67,78]
for i in a:
print i
sleep(1)
if __name__ == "__main__":
Yep = myClass()
thread = Thread(target = Yep.help)
thread2 = Thread(target = Yep.nope)
thread.start()
thread2.start()
thread.join()
print "Finished"
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Creating Threads in python, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Creating Threads in python
- Deutsch Creating Threads in python
- Français Creating Threads in python
- Español Creating Threads in python
- Türk Creating Threads in python
- Русский Creating Threads in python
- Português Creating Threads in python
- Polski Creating Threads in python
- Nederlandse Creating Threads in python
- 中文 Creating Threads in python
- 한국어 Creating Threads in python
- 日本語 Creating Threads in python
- हिन्दी Creating Threads in python
New York | 2023-04-01
Thanks for explaining! I was stuck with Creating Threads in python for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Paris | 2023-04-01
code Python module is always a bit confusing 😭 Creating Threads in python is not the only problem I encountered. Will use it in my bachelor thesis
Vigrinia | 2023-04-01
Thanks for explaining! I was stuck with Creating Threads in python for some hours, finally got it done 🤗. I am just not quite sure it is the best method