学习Python5(Thread)

测试代码

#coding:utf8
import thread, time, random

print '-'*25, '测试1', '-'*25
count = 0
lock = thread.allocate_lock()
def threadTest():
    global count, lock
    lock.acquire()
    for i in xrange(1000000):
        count += 1
    lock.release()
for i in range(5):
    thread.start_new_thread(threadTest, ()) #如果对start_new_thread函数不是很了解,不要着急,马上就会讲解
    # print thread.get_ident()
    # print thread.stack_size()
    time.sleep(0.1)
print count #count是多少呢?是10000 * 10 吗?

# print '-'*25, '测试2', '-'*25
# def threadFunc(a = None, b = None, c = None, d = None):
#     print time.strftime('%H:%M:%S', time.localtime()), a
#     time.sleep(1)    
#     print time.strftime('%H:%M:%S', time.localtime()), b
#     time.sleep(1)
#     print time.strftime('%H:%M:%S', time.localtime()), c
#     time.sleep(1)
#     print time.strftime('%H:%M:%S', time.localtime()), d
#     time.sleep(1)
#     print time.strftime('%H:%M:%S', time.localtime()), 'over'

# thread.start_new_thread(threadFunc, (3, 4, 5, 6))   #创建线程,并执行threadFunc函数。
# time.sleep(5)

# print '-'*25, '测试3:', '-'*25
# thread.start_new_thread(lambda : (thread.interrupt_main(), ), ())
# try:
#     time.sleep(2)
# except KeyboardInterrupt, e:
#     print 'error:', e
# print 'over'

测试结果

------------------------- 测试1 -------------------------
5000000
>>> 
多进程执行是无序的: ```python import multiprocessing import time def show(): time.sleep(2) print(multiprocessing.current_process().name) if __name__ == '__main__': for _ in range(5): p = multiprocessing.Process(target=show) p.start() ``` 手动销毁子进程: ```python import multiprocessing import time def task(): for i in range(10): print('任务进行中...') time.sleep(0.2) if __name__ == '__main__': p1 = multiprocessing.Process(target=task) p1.start() time.sleep(0.5) print('主进程中最后一行代码....') p1.terminate() ``` 多线程join解决线程安全问题: ```python import threading sum = 0 def sum1(): global sum for i in range(1000000): sum += 1 print(sum) def sum2(): global sum for i in range(1000000): sum += 1 print(sum) if __name__ == '__main__': t1 = threading.Thread(target=sum1) t2 = threading.Thread(target=sum2) t1.start() t1.join() t2.start() ``` 进程和线程对比: 多任务学习Python代码可以采用多进程或多线程的方式来实现。多进程可以通过创建进程对象并开启进程来同时执行多个任务,而多线程可以通过创建线程对象并开启线程来实现。在多线程中,为了解决线程安全问题可以使用join方法。不同的是,多进程可以利用terminate方法手动销毁子进程,而多线程没有这个方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python多任务教程](https://blog.csdn.net/qdPython/article/details/129028153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值