1.方法一    http://hi.baidu.com/javalang/item/72fabf2359a30b464799625e

也就是说当线程使用start方法运行起来后,只有当run方法运行结束,一个线程才会结束。

import threading
from threading import Thread
import time
class MyThread(Thread):
    over=False
    def __init__(self):
        Thread.__init__(self)

def run(self):
        while not self.over:
            print "hello"
            time.sleep(2)
    def kill(self):
        self.over=True

if __name__=='__main__':
    t=MyThread()
    t.start()
    print 'wait 5s'
    time.sleep(5)
    t.kill()

该方法适合run()方法中不包含死循环程序,若run()包含死循环,则此方法无法结束该线程

2.方法二

import threading
import thread
import time
import os
import urllib

def timer():
    time_start = time.time()
    while(1):
    time_end = time.time()
    if ((time_end - time_start) > 4):
        return 1

def printf():
    i = 0
    while(1):
        i = i + 1
        print i
        time.sleep(1)

def run():
    sub_thread1 = thread.start_new_thread(printf,())
    try:
    if(timer()):
        sub_thread1.exit()
    except AttributeError:
        pass
    print "stop\n"
    print "success\n"

if __name__=='__main__':
    run()

开一个线程,该线程为死循环,则使用Thread类的exit方法可以退出,一般情况建议使用Thread.threading

若子线程触发了一个新的进程,则此方法无效,需使用父进程杀死该子进程

方法三:

import threading
import thread
import time
import os
import urllib

def timer():
    time_start = time.time()
    while(1):
    time_end = time.time()
    if ((time_end - time_start) > 10):
        return 1

def printf():
    i = 0
    while(1):
        i = i + 1
        print i
        time.sleep(1)
        #if(i > 15):
        # break

def run(filename):
    sub_thread = thread.start_new_thread(os.system,('java -jar'+' '+ filename,))
    try:
        if(timer()):
        sub_thread.exit()
        except AttributeError:
            pass
        tasks = os.popen('jps -m').readlines()
        #print tasks
         found_task = ""
         PID_end_position = 0
         for task in tasks:
             if filename[3:] in task:
                 found_task = task
                 PID_end_position = found_task.find(filename[3:]) - 1
                 break
             if found_task:
             PID = found_task[0 : PID_end_position]
             os.system("taskkill /f /PID " + PID)
             time.sleep(3)
             os.system('del /f'+' '+filename)

if __name__=='__main__':
            run('D:\\ba-finance-tuangou-bp-job-1.0.1-SNAPSHOT.jar')

Python中进程无法结束的处理办法的更多相关文章

  1. Python中进程

    程序 程序:编写完的代码称为程序. 进程 进程:又称重量级进程,正在执行中的程序称为进程.进程的执行会占用内存等资源.多个进程同时执行时,每个进程的执行都需要由操作系统按一定的算法(RR调度.优先数调 ...

  2. day 27 Python中进程的操作

    进程的创建和结束: multiprocess模块: multiprocess不是一个模块而是python中一个操作.管理进程的包 分为四个部分:创建进程部分,进程同步部分,进程池部分,进程之间数据共享 ...

  3. python中进程、线程、协程简述

    进程 python中使用multiprocessing模块对进程进行操作管理 进程同步(锁.信号量.事件) 锁 —— multiprocessing.Lock 只要用到了锁 锁之间的代码就会变成同步的 ...

  4. python中进程间通讯——文件锁之fcntl模块的使用

    python 中给文件加锁——fcntl模块import fcntl 打开一个文件##当前目录下test文件要先存在,如果不存在会报错.或者以写的方式打开f = open('./test')对该文件加 ...

  5. python c++ 混合编程中python调用c++string返回类型的函数,python中返回为数字的解决办法

    本随笔解决 Python使用ctypes 调用c++dll 字符串返回类型函数,在python中显示为数字:原文解决方案见so: https://stackoverflow.com/questions ...

  6. Python中进程和线程的总体区别

    Num01–>线程 线程是操作系统中能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一个线程指的是进程中一个单一顺序的控制流. 一个进程中可以并发多条线程,每条线程并行 ...

  7. python中进程池的应用

    #原创,转载请联系 假设我们写的一个程序需要运行100个子进程的时候,那么写程序时,不可能循环创建销毁100个进程吧?进程的创建与销毁是很耗系统的资源的. 进程池的作用就体现出来了. 进程池可以控制进 ...

  8. python中进程池和回调函数

    一.数据共享 1.进程间的通信应该尽量避免共享数据的方式 2.进程间的数据是独立的,可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实 ...

  9. python中进程详解

    1:pdb调试:基于命令行的调试工具,非常类似gnu和gdb调试,以下是常用的调试命令: 可以python -m pdb xxx.py(你的py文件名)进入命令行调试模式 命令 简写命令 作用 bea ...

随机推荐

  1. 宋牧春: Linux设备树文件结构与解析深度分析(2) 【转】

    转自:https://mp.weixin.qq.com/s/WPZSElF3OQPMGqdoldm07A 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...

  2. Nginx部署部分https与部分http【转】

    转自 Nginx部署部分https与部分http - na_tion的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/na_tion/article/details/ ...

  3. 【转载】在GitHub上管理项目

    在GitHub上管理项目 新建repository 本地目录下,在命令行里新建一个代码仓库(repository) 里面只有一个README.md 命令如下: touch README.md git ...

  4. Oracle安装出现报错

    报错信息如下: >>> Couldnot execute auto check for display colors using command /usr/bin/xdpyinfo. ...

  5. redis局域网内开启访问

    若需要开启A(192.168.0.3)的访问1.配置confg bind 192.168.0.3 2.设置访问密码 requirepass password 3.重新载入配置 ./redis-serv ...

  6. 间隔查询显示命令watch

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  7. linux c下输入密码不回显

    今天做一个登录程序,需要屏蔽掉密码,于是自己就在网上找资料,找到了一种和linux终端下输入密码方式相同的方法,不显示在终端,具体代码实现如下. #include<stdio.h> #in ...

  8. find tar排除指定文件或目录操作及查找文件内容关键字

    1.find查找排除单个目录 查找当前目录或者子目录下所有.txt文件,但是跳过子目录sk find . -path "./sk" -prune -o -name "*. ...

  9. 洛谷 P2369 EXCEEDED WARNING A 题解

    题目传送门 直接用sort排序最后输出即可.但是数组要使用short int 类型.否则会超内存. #include<bits/stdc++.h> using namespace std; ...

  10. 【严蔚敏】【数据结构题集(C语言版)】1.16 自大至小依次输出读入的三个整数X,Y,Z

    #include <stdio.h> #include<stdlib.h> int main() { int x,y,z,temp; scanf("%d%d%d&qu ...