国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

探尋python多線程ctrl+c退出問題解決方案

2020-02-23 06:07:00
字體:
來源:轉載
供稿:網友

場景:

經常會遇到下述問題:很多io busy的應用采取多線程的方式來解決,但這時候會發現python命令行不響應ctrl-c 了,而對應的java代碼則沒有問題:

代碼如下:
public class Test { 
    public static void main(String[] args) throws Exception { 
 
        new Thread(new Runnable() { 
 
            public void run() { 
                long start = System.currentTimeMillis(); 
                while (true) { 
                    try { 
                        Thread.sleep(1000); 
                    } catch (Exception e) { 
                    } 
                    System.out.println(System.currentTimeMillis()); 
                    if (System.currentTimeMillis() - start > 1000 * 100) break; 
                } 
            } 
        }).start(); 
 
    } 

java Test

ctrl-c則會結束程序

而對應的python代碼:

代碼如下:
# -*- coding: utf-8 -*- 
import time 
import threading 
start=time.time() 
def foreverLoop(): 
    start=time.time() 
    while 1: 
        time.sleep(1) 
        print time.time() 
        if time.time()-start>100: 
            break 
              
thread_=threading.Thread(target=foreverLoop) 
#thread_.setDaemon(True) 
thread_.start() 

python p.py

后ctrl-c則完全不起作用了。

不成熟的分析:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北川| 修文县| 灵石县| 肇州县| 错那县| 南川市| 沧州市| 尼玛县| 谷城县| 开原市| 绿春县| 武城县| 彝良县| 忻州市| 临朐县| 寿阳县| 固镇县| 攀枝花市| 体育| 斗六市| 灵石县| 延边| 双桥区| 丹寨县| 龙州县| 临高县| 松阳县| 丰原市| 涟源市| 砀山县| 黄浦区| 通榆县| 田林县| 浮山县| 公安县| 广宁县| 临武县| 宁阳县| 宜城市| 塔河县| 中卫市|