Следующая программа Python использует модуль psutil для получения списка процессов, которые принадлежат вам, проверяет, запущен ли известный браузер, и, если да, запускает его снова. Если браузер не может быть найден, запускается браузер по умолчанию. Я добавил несколько комментариев, чтобы прояснить, что происходит в сценарии.
Помимо самого сценария, вам все равно нужно сделать этот исполняемый файл с помощью chmod и убедиться, что сценарий выполняется вместо запуска определенного браузера.
#!/usr/bin/env python
import psutil
import os
import subprocess
import sys
import pwd
def getlogin():
return pwd.getpwuid(os.geteuid()).pw_name
def start_browser(exe_path):
# Popen should start the process in the background
# We'll also append any command line arguments
subprocess.Popen(exe_path + sys.argv[1:])
def main():
# Get our own username, to see which processes are relevant
me = getlogin()
# Process names from the process list are linked to the executable
# name needed to start the browser (it's possible that it's not the
# same). The commands are specified as a list so that we can append
# command line arguments in the Popen call.
known_browsers = {
"chrome": [ "google-chrome-stable" ],
"chromium": [ "chromium" ],
"firefox": [ "firefox" ]
}
# If no current browser process is detected, the following command
# will be executed (again specified as a list)
default_exe = [ "firefox" ]
started = False
# Iterate over all processes
for p in psutil.process_iter():
try:
info = p.as_dict(attrs = [ "username", "exe" ])
except psutil.NoSuchProcess:
pass
else:
# If we're the one running the process we can
# get the name of the executable and compare it to
# the 'known_browsers' dictionary
if info["username"] == me and info["exe"]:
print(info)
exe_name = os.path.basename(info["exe"])
if exe_name in known_browsers:
start_browser(known_browsers[exe_name])
started = True
break
# If we didn't start any browser yet, start a default one
if not started:
start_browser(default_exe)
if __name__ == "__main__":
main()
Если вы работаете с ядром, у вас должна быть подписка на Еженедельные новости Linux .
В результате очень быстрого поиска была обнаружена эта статья , в которой упоминается, что aio_read
и aio_write
обрабатываются read_iter
и ] write_iter
.
Вы также можете найти краткое заявление на этот счет в Documentation / filesystems / porting
.