python主线程主动杀死子线程_是否有可能从python中的子线程中杀死父线程?

在Python中,想要限制脚本运行时间并超时时抛出异常,可以使用信号模块或者上下文管理器。一种可能的解决方案是使用`signal`模块设置一个信号处理器,当达到指定时间时发送信号,然后在主程序中捕获这个信号来终止执行。另一种方法是利用`contextlib`创建一个上下文管理器,结合`time`模块来实现超时控制。这种方法可以确保即使子任务耗时过长,也能正确地中断。
摘要由CSDN通过智能技术生成

我在

Windows上使用

Python 3.5.2.

我想运行一个python脚本,但保证它不会超过N秒.如果确实需要超过N秒,则应引发异常,程序应退出.最初我以为我可以在开始时启动一个线程,在抛出异常之前等待N秒,但这只会设置为向计时器线程抛出异常,而不是父线程.例如:

import threading

import time

def may_take_a_long_time(name, wait_time):

print("{} started...".format(name))

time.sleep(wait_time)

print("{} finished!.".format(name))

def kill():

time.sleep(3)

raise TimeoutError("No more time!")

kill_thread = threading.Thread(target=kill)

kill_thread.start()

may_take_a_long_time("A", 2)

may_take_a_long_time("B", 2)

may_take_a_long_time("C", 2)

may_take_a_long_time("D", 2)

这输出:

A started...

A finished!.

B started...

Exception in thread Thread-1:

Traceback (most recent call last):

File "C:\Program Files\Python35\lib\threading.py", line 914, in _bootstrap_inner

self.run()

File "C:\Program Files\Python35\lib\threading.py", line 862, in run

self._target(*self._args, **self._kwargs)

File "timeout.py", line 11, in kill

raise TimeoutError("No more time!")

TimeoutError: No more time!

B finished!.

C started...

C finished!.

D started...

D finished!.

这甚至可以远程实现吗?我意识到我可以这样做:

import threading

import time

def may_take_a_long_time(name, wait_time, thread):

if not thread.is_alive():

return

print("{} started...".format(name))

time.sleep(wait_time)

print("{} finished!.".format(name))

def kill():

time.sleep(3)

raise TimeoutError("No more time!")

kill_thread = threading.Thread(target=kill)

kill_thread.start()

may_take_a_long_time("A", 2, kill_thread)

may_take_a_long_time("B", 2, kill_thread)

may_take_a_long_time("C", 2, kill_thread)

may_take_a_long_time("D", 2, kill_thread)

但是,如果调用may_take_a_long_time(“B”,60,kill_thread),则此方法将失败.

所以我想我的TL; DR问题是,对主线程本身设置时间限制的最佳方法是什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python线程编程线程绑定 GPU 失败的问题可以通过以下几种方式解决: 1. 在每个线程都调用 `torch.cuda.set_device()` 显式地设置要使用的 GPU 设备。这可以确保在每个线程使用的 CUDA 上下文与线程的一致。例如: ```python import torch import threading def worker(): torch.cuda.set_device(0) # 设置要使用的 GPU 设备 # 在线程进行 GPU 相关的操作 # 创建线程并启动 thread = threading.Thread(target=worker) thread.start() ``` 2. 将需要在线程进行的 GPU 相关操作放在一个函数,然后通过 `torch.cuda.stream()` 创建一个新的 CUDA 流,并将操作放入该流执行。这样可以避免多个线程之间的 CUDA 上下文冲突。例如: ```python import torch import threading def worker(): # 创建一个新的 CUDA 流 stream = torch.cuda.stream() with torch.cuda.stream(stream): # 在线程进行 GPU 相关的操作 # 创建线程并启动 thread = threading.Thread(target=worker) thread.start() ``` 3. 使用 `torch.cuda.device()` 上下文管理器手动管理 CUDA 上下文。这样可以确保在线程结束时正确释放 CUDA 资源。例如: ```python import torch import threading def worker(): with torch.cuda.device(0): # 设置要使用的 GPU 设备 # 在线程进行 GPU 相关的操作 # 创建线程并启动 thread = threading.Thread(target=worker) thread.start() ``` 需要注意的是,多线程环境下使用 GPU 时,要确保正确地管理 CUDA 上下文,避免出现资源泄漏或冲突的问题。并且要根据具体情况选择适合的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值