国产探花免费观看_亚洲丰满少妇自慰呻吟_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則完全不起作用了。

不成熟的分析:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅江市| 榆中县| 西乡县| 甘德县| 大同县| 寿阳县| 黑龙江省| 康保县| 航空| 勃利县| 秭归县| 太谷县| 汤阴县| 安康市| 承德县| 阿拉善左旗| 库尔勒市| 大城县| 盐池县| 石屏县| 武鸣县| 攀枝花市| 桐城市| 东丽区| 嘉祥县| 胶南市| 宁强县| 湘乡市| 哈密市| 清水县| 五寨县| 屏边| 丹阳市| 福安市| 德化县| 清流县| 宾川县| 定襄县| 安吉县| 荣昌县| 河西区|