I'm trying to host a music bot on repl.it and lavalink keeps terminating on. How do I stop it from terminating.
I had this problem for a few weeks. The solution is very simple.
import os
import time
import threading
def run_lavalink():
os.system("java -jar Lavalink.jar")
threading.Thread(target=run_lavalink).start()
time.sleep(20) #wait until lavalink is ready up
#bot.run and other stuffs
Related
PyCharm terminal freezes (or loops) when executing the file and the computer starts to warm up.
Code that doesn't run:
import time
import requests
start_time = time.time()
print(start_time)
Code that works:
Code that doesn't run:
import time
# import requests
start_time = time.time()
print(start_time)
The problem occurs when some libraries are present, for example requests, pycoingecko, pymongo. I assume that these are the libraries that access the Internet.
The problem occurred unexpectedly, the settings in the program did not change.
Everything worked before.
System: Ubuntu.
I need to program in py 2.7 a function that
when active, it closes all connections to the internet of a client (It is a server-client socket connection)
Because I cannot just make the client turn off the internet (Because then we won't be able to communicate with him)
I thought to run a thread. That when active (When the server sends to the client "close_internet") the thread runs a while true function that closes all the browsers (and thus "closing" its internet connection). I will have a list of known browsers (not all) that the thread will actively close.
This line of code closes any process by name:
import os
import threading
def close_internet():
while 1:
try:
os.system('taskkill /f /im MicrosoftEdge.exe')
except:
pass
threading.Thread(target=close_internet, args=()).start()
When I run this code, it does work, closing explorer when I open it (Explorer just for testing, you can change to chrome.exe or firefox.exe)
However, in the console it prints some scary errors, all the time:
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
What can I do? I only need this program to work in the background, without the user noticing with those errors.
The errors appear (not surprisingly) when I don't have explorer open.
When I do have it open, it works fine and prints:
ERROR: The process "MicrosoftEdge.exe" not found.
ERROR: The process "MicrosoftEdge.exe" not found.
SUCCESS: The process "MicrosoftEdge.exe" with PID 10808 has been terminated. # <-- this line
ERROR: The process "MicrosoftEdge.exe" not found.
The function is programmed on the client-side!
How can I fix it?
And secondly, is this the right way to close a process by its name? is it efficient? or is there a more efficient way to close a process (Maybe using its PID makes it faster...) I'd like to hear your advice.
Thanks!
Try using the subprocess module. I don't think you need Threading for this task.
Here is the code i use to open windows explorer and highlight a file.
subprocess.Popen(r'explorer /select, %s%s'%(self.cwd, ofile))
It should be easy to modify for your task. Also that code is assuming that Edge is open and the name is MicrosoftEdge.exe is the process name. Assume nothing and see if you can get a list of running processes using python, scan through that list and check if the application you want to close is already open. Here is a link with some information on getting the list of processes running on windows.
I need to get the particular process cpu usage from task manager using python.
How can i achieve this?
You can use psutil library.
For getting process
import psutil
p = psutil.Process(<ProcessID>)
print(p.cpu_percent(interval=1.0))
This will give you the float representation of the current system-wide CPU utilization as a percentage by a process.
Also if you are having any trouble in retrieving any running process PID you can again use psutil library as :
import psutil
for proc in psutil.process_iter():
if proc.name() == <Some Running Process Name> :
try:
pinfo = proc.as_dict(attrs=['pid'])
except psutil.NoSuchProcess:
pass
else:
print(pinfo)
It will give you pid inside a dictionary thou.
I'm trying to build my application into an .app file and I kept hitting the following error.
[Errno 2] No such file or directory: '/Library/Frameworks/Tcl.framework/Versions/8.5/Tcl'
So I downloaded ActiveTcl8.5 and the error went away. Now I'm able to build using the following command:
python3 setup_cx_freeze.py bdist_dmg
But my application runs and immediately closes. I ran it from the cmd to get a sense as to what the error is and the only feedback I'm getting is LSOpenURLsWithRole() failed with error -10810. What am I doing wrong? Why did I need Tcl in order to use cx freeze and why does my app not want to open? This is my setup file.
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ['tkinter', 'smb.SMBConnection'], "excludes": [], "includes": [], "include_files":['Project_Images', 'Project_Docs'], "bin_path_excludes": []}
setup(
name = "Test",
version = "2.51",
description = "Will this even work?",
options = {"build_exe": build_exe_options},
executables = [Executable("AccuAdmin.py")])
Tcl is a programming language, invented at roughly the same time as Python. Tkinter is a thin wrapper on top of an embedded tcl interpreter with the "tk" widget extension. You cannot use tkinter without tcl/tk.
I'm attempting to install a windows service with Python (3.4). Upon installation I intend to run it. It doesn't perform any function other than demo a service running in Windows.
I'm getting the following access permission when installing the service:
I'm the admin for the computer therefore I should have the permission to do this.
Perhaps its because the command line is attempting install the service with a Python. Does Python have permission to do this through the command line?
How can I get past this issue. Is there a specific file with permissions that I need to change?
I've included the code from the service just in case.
The help is appreciated.
#Run a Windows Service
import win32serviceutil
import win32service
import win32event
import os
import sys
import time
from threading import Thread
import http.server
class ServiceLauncher(win32serviceutil.ServiceFramework):
_svc_name_ = "PythonService"
_svc_display_name_ = "Python based win32 service"
_svc_description_ = ""
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
thread = Thread(target = httpserver.run_httpserver)
thread.daemon = True
thread.start()
while (1):
rc = win32event.WaitForSingleObject(self.hWaitStop, 1000)
if rc==win32event.WAIT_OBJECT_0:
# Stop event
break
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(ServiceLauncher)
The solution involves running the command line as an administrator. This is performed by right clicking the command prompt and selecting to run the command line as an administrator.
In Windows, the user logged into the computer may be a Windows administrator but the rights do not automatically extend to the command line. The user with administrator rights has to choose to run the command line as an administrator in order to perform the commands reserved to administrators such as performing an install of a Windows service.