python 唤醒进程_python2设置进程实时性

#! /usr/bin/env python

# # -*- coding: utf-8 -*-

from __future__ import absolute_import, print_function

import time

import logging

import sys

try:

import ctypes

import ctypes.util

c = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))

importCtypesFailed = False

except Exception:

importCtypesFailed = True

logging.debug("rush() not available because import ctypes, ctypes.util "

"failed in psychopy/platform_specific/linux.py")

# FIFO and RR(round-robin) allow highest priority for realtime

SCHED_NORMAL = 0

SCHED_FIFO = 1

SCHED_RR = 2

SCHED_BATCH = 3

if not importCtypesFailed:

class _SchedParams(ctypes.Structure):

_fields_ = [('sched_priority', ctypes.c_int)]

warnMax = """Could not raise thread priority with sched_setscheduler.

To enable rush(), if you are using a debian-based Linux, try this:

'sudo setcap cap_sys_nice=eip %s' [NB: install 'setcap' first.]

If you are using the system's python (eg /usr/bin/python2.x), its highly

recommended to change cap_sys_nice back to normal afterwards:

'sudo setcap cap_sys_nice= %s'"""

warnNormal = ("Failed to set thread priority to normal with "

"sched_setscheduler.\n"

"Try: 'sudo setcap cap_sys_nice= %s'")

def rush(value=True, realtime=False):

"""Raise the priority of the current thread/process using

- sched_setscheduler

realtime arg is not used in Linux implementation.

NB for rush() to work on (debian-based?) Linux requires that the

script is run using a copy of python that is allowed to change

priority, eg: sudo setcap cap_sys_nice=eip ,

and maybe restart PsychoPy. If is the system python,

it's important to restore it back to normal to avoid possible

side-effects. Alternatively, use a different python executable,

and change its cap_sys_nice.

For RedHat-based systems, 'sudo chrt ...' at run-time might be

needed instead, not sure.

see http://rt.et.redhat.com/wiki/images/8/8e/Rtprio.pdf

"""

if importCtypesFailed:

return False

if value: # set to RR with max priority

schedParams = _SchedParams()

schedParams.sched_priority = c.sched_get_priority_max(SCHED_RR)

err = c.sched_setscheduler(0, SCHED_RR, ctypes.byref(schedParams))

print("err ", err)

if err == -1: # returns 0 if OK

logging.warning(warnMax % (sys.executable, sys.executable))

else: # set to RR with normal priority

schedParams = _SchedParams()

schedParams.sched_priority = c.sched_get_priority_min(SCHED_NORMAL)

err = c.sched_setscheduler(0, SCHED_NORMAL, ctypes.byref(schedParams))

if err == -1: # returns 0 if OK

logging.warning(warnNormal % sys.executable)

return True

rush()

while True:

print("aa")

time.sleep(1)

pass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值