1.进程测试1
import os
import threading
# def echo_number():
# for i in range()
import time
import threading
def dance():
for i in range(5):
print("---dancing---")
time.sleep(1)
print(time.ctime())
def sing():
for i in range(5):
print("---singing---")
time.sleep(1)
print(time.ctime())
def main():
t1 = threading.Thread(target = dance)
t2 = threading.Thread(target = sing)
t1.start()
t2.start()
if __name__ == '__main__':
main()
2.test2
import time
import threading
def echo_sen(sentence):
print(sentence)
time.sleep(1)
print(time.ctime())
# for i in range(5):
# print("---dancing---")
# time.sleep(1)
# print(time.ctime())
# def sing():
# for i in range(5):
# print("---singing---")
# time.sleep(1)
# print(time.ctime())
echo_sen('sentence')
list1=['hello','bye']
# def step1():
# if __name__ == '__main__':
# t1 = threading.Thread(target = echo_sen,args=('hello'))
# t2 = threading.Thread(target = echo_sen,args=('bye'))
# t1.start()
# t2.start()
# echo_sen('hello')
# echo_sen('bye')
for item in list1:
sub_thread=threading.Thread(target=echo_sen,args=(item,))
sub_thread.start()
# t1 = threading.Thread(target=echo_sen, args=('hello'))
# t1.start()
# if __name__ == '__main__':
# main()
3.real_use
import time
import threading
def echo_sen(sentence):
print(sentence)
time.sleep(1)
print(time.ctime())
list1=['hello','bye']
if __name__=='__main__':
for item in list1:
sub_thread=threading.Thread(target=echo_sen,kwargs={'sentence':item})
sub_thread.start()