import _thread
from time import sleep
from datetime import datetime
date_time_format='%y-%M-%d %H:%M:%S'
def date_time_str(date_time):
return datetime.strftime(date_time,date_time_format)
def loop_one():
print('+++线程一开始于:',date_time_str(datetime.now()))
print('+++线程一休眠4秒')
sleep(4)
print('+++线程一休眠结束,结束于:'.date_time_str(datetime.now()))
def loop_two():
print('***线程二开始于:',date_time_str(datetime.now()))
print('***线程二休眠2秒')
sleep(2)
print('***线程二休眠结束,结束于:'.date_time_str(datetime.now()))
def main():
print('-----所有线程开始时间:',date_time_str(datetime.now()))
_thread.start_new_thread(loop_one,())
_thread.start_new_thread(loop_two,())
sleep(6)
print('-----所有线程结束时间:',date_time_str(datetime.now()))
if name=='__main__': #__name__是所有模块的内建属性
main()
编译时出现下列问题,求解答
Unhandled exception in thread started by
line 21, in loop_two
print('***线程二休眠结束,结束于:'.date_time_str(datetime.now()))
AttributeError: 'str' object has no attribute 'date_time_str'
line 15, in loop_one
print('+++线程一休眠结束,结束于:'.date_time_str(datetime.now()))
AttributeError: 'str' object has no attribute 'date_time_str'
print('***线程二休眠结束,结束于:'.date_time_str(datetime.now()))
函数前面的点改成逗号,手误吧亲- -
https://www.cnblogs.com/smallmars/p/7149507.html
点是方法调用,'***线程二休眠结束,结束于:'.date_time_str(datetime.now()) 相当于'***线程二休眠结束,结束于:'调用了date_time_str(datetime.now()) 方法,字符串连接使用+号