Python通过两个标准库thread和threading提供对线程的支持。thread提供了低级别的、原始的线程以及一个简单的锁。
thread 模块提供的其他方法:
thread.start_new_thread ( function, args[, kwargs] )
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import thread
import time
def execute(thread_name, delay, counts):
for i in range(counts):
time.sleep(delay)
print(" %s %s" % (thread_name, time.ctime(time.time())))
def main():
try:
thread.start_new_thread(execute, ('thread_1', 1, 3))
thread.start_new_thread(execute, ('thread_2', 2, 3))
except:
print('cannot start thread')
if __name__ == '__main__':
main()
time.sleep(10)
threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
target就是线程函数,name是线程名,args是传给target的参数,kwargs是字典类型的参数
import threading
import time
def execute(delay, counts):
for i in range(counts):
time.sleep(delay)
print("%s %s" % (threading.current_thread().name, time.ctime(time.time())))
def main():
try:
thread_1 = threading.Thread(target=execute, name='thread_1', args=(1, 3))
thread_2 = threading.Thread(target=execute, name='thread_2', args=(2, 3))
except:
print('cannot start thread')
thread_1.start()
thread_2.start()
thread_1.join()
thread_2.join()
if __name__ == '__main__':
main()
import threading
import time
class MyThread(threading.Thread):
"""docstring for MyThread"""
def __init__(self, thread_name, delay, counts):
super(MyThread, self).__init__()
self.thread_name = thread_name
self.delay = delay
self.counts = counts
def run(self):
for i in range(self.counts):
time.sleep(self.delay)
print('%s %s' % (self.thread_name, time.ctime(time.time())))
def main():
thread_1 = MyThread('thread_1', 1, 3)
thread_2 = MyThread('thread_2', 2, 3)
thread_1.start()
thread_2.start()
if __name__ == '__main__':
main()
import threading
import time
class Account(object):
"""docstring for Account"""
def __init__(self, money):
super(Account, self).__init__()
self.money = money
def withdraw(self, number):
if number < 0:
return -1
elif self.money < 0:
return -2
elif number > self.money:
return -3
else:
time.sleep(1)
self.money -= number
print('withdraw %d remain %d' % (number, self.money))
return number
class User(threading.Thread):
"""docstring for MyThread"""
def __init__(self, thread_name, account):
super(User, self).__init__()
self.thread_name = thread_name
self.Account = account
def run(self):
self.get_money(800)
def get_money(self, number):
print('%s get %d' % (self.thread_name, self.Account.withdraw(number)))
def main():
account = Account(1000)
user_1 = User('User_1', account)
user_2 = User('User_2', account)
user_1.start()
user_2.start()
if __name__ == '__main__':
main()
银行账户取款问题,两个用户使用同一个账户取款,比如一个人拿存折去银行取,一个人拿卡去取款机取,这是可能存在的.账户里总共有1000元,但是两个人都要取800,正常来说是一个人去了800后另外一个人是取不出的,但是运行的结果是:
withdraw 800 remain 200
User_1 get 800
withdraw 800 remain -600
User_2 get 800
两个用户都取出了800.这是为什么呢?因为在钱数减少前休眠了一个简短的时间,可能一个用户进入休眠后,由于钱并为减少,所以另一个用户也能进入,所以两个用户都能取走钱.
import threading
import time
class Account(object):
"""docstring for Account"""
def __init__(self, money):
super(Account, self).__init__()
self.money = money
def withdraw(self, number):
if number < 0:
return -1
elif self.money < 0:
return -2
elif number > self.money:
return -3
else:
time.sleep(1)
self.money -= number
print('withdraw %d remain %d' % (number, self.money))
return number
class User(threading.Thread):
"""docstring for MyThread"""
def __init__(self, thread_name, account, lock):
super(User, self).__init__()
self.thread_name = thread_name
self.Account = account
self.thread_lock = lock
def run(self):
self.thread_lock.acquire()
self.get_money(800)
self.thread_lock.release()
def get_money(self, number):
print('%s get %d' % (self.thread_name, self.Account.withdraw(number)))
def main():
threadLock = threading.Lock()
account = Account(1000)
user_1 = User('User_1', account, threadLock)
user_2 = User('User_2', account, threadLock)
user_1.start()
user_2.start()
if __name__ == '__main__':
main()
运行结果:
withdraw 800 remain 200
User_1 get 800
User_2 get -3
只有一个用户能取出钱.