So basically, i have made a tkinter app that has a reminder utiliy in specific to generate notifications at the scheduled time. Everything works fine until I run the app module and another module having the notification generating function one at a time , but when I call the notification generating function intto the app module, my app doesnt work but the notification works. I want the app to run such that the notification generating function kind of runs in the background until the app module is open.
github link: https://github.com/click-boom/Trella
Looking into chatgpt i found terms like threading and multiprocessing, but i have no concept of that and still tried but didnt work.
Sure enough what you are looking for is multithreading.
Here is a simple example of how multithreading works (sorry for my lack of drawing skills).
This is how all monothread programs work. In most programming languages this is the default behaviour.
So in this example Second Task will have to wait for First Task to complete.
If you want several tasks to run concurrently, you can use multithreading.
This is how you could implement this in Python.
Monothreading:
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
firstTask()
secondTask()
Here, secondTask will only run after firstTask is done (i.e after 10 seconds).
Multithreading:
from threading import Thread
from time import sleep
def firstTask():
time = 10
for i in range(time):
sleep(1)
print(f'I have been running for {i}s')
def secondTask():
print('All I want to do is run once')
first_thread = Thread(target=firstTask)
second_thread = Thread(target=secondTask)
first_thread.start()
second_thread.start()
I hope this will be a help to someone !
I created a script for automating a system. But I am stuck with the hosting the script and with the time. Lets discuss about the time first.
Here is my code:
from ray import SMSBot
import schedule
with SMSBot() as sms:
def execute():
sms.go_to_dashboard("https://sms.rayadvertising.com/login")
sms.login_to_dashboard("*****", "*****")
sms.find_number_section()
schedule.every(1).minute.do(execute())
schedule.every(20).minute.do(execute())
schedule.every(2).hour.do(execute())
schedule.every(12).hour.do(execute())
schedule.every(24).hour.do(execute())
schedule.every(26).hour.do(execute())
I tried this code. But I am afraid of taking this script on deployment.
What I want:
I want the script run after 5 min then 20 min then 2 hour then 12 hour then 24 hour then 26 hour.
how can I do that? Is my code ok?
second thing is I want to host it. Where and how can I host this script. I am running ubuntu in laptop I know I can run the script in my laptop but its not possible to keep running the laptop whole day.
Is there any help for me?
I am trying to launch a steam game on my computer through an ssh connection (into a Win10 machine). When run locally, the following python call works.
subprocess.run("start steam://rungameid/[gameid]", shell=True)
However, whenever I run this over an ssh connection—either in an interactive interpreter or by invoking a script on the target machine—my steam client suddenly exits.
I haven't noticed anything in the steam logs except that Steam\logs\connection_log.txt contains logoff and a new session start each time. This is not the case when I run the command locally on my machine. Why is steam aware of the different sources of this command, and why is this causing the steam connection to drop? Can anyone suggest a workaround?
Thanks.
Steam is likely failing to launch the application because Windows services, including OpenSSH server, cannot access the desktop, and, hence, cannot launch GUI applications. Presumably, Steam does not expect to run an application in an environment in which it cannot interact with the desktop, and this is what eventually causes Steam to crash. (Admittedly, this is just a guess—it's hard to be sure exactly what is happening when the crash does not seem to appear in the logs or crash dumps.)
You can see a somewhat more detailed explanation of why starting GUI applications over SSH fails when the server is run as a Windows service in this answer by domih to this question about running GUI applications over SSH on Windows.
domih also suggests some workarounds. If it is an option for you, the simplest one is probably to download and run OpenSSH server manually instead of running the server as a service. You can find the latest release of Win32-OpenSSH/Windows for OpenSSH here.
The other workaround that still seems to work is to use schtasks. The idea is to create a scheduled task that runs your command—the Task Scheduler can access the desktop. Unfortunately, this is only an acceptable solution if you don't mind waiting until the next minute at least; schtasks can only schedule tasks to occur exactly on the minute. Moreover, to be safe to run at any time, code should probably schedule the task for at least one minute into the future, meaning that wait times could be anywhere between 1–2 minutes.
There are also other drawbacks to this approach. For example, it's probably harder to monitor the running process this way. However, it might be an acceptable solution in some circumstances, so I've written some Python code that can be used to run a program with schtasks, along with an example. The code depends on the the shortuuid package; you will need to install it before trying the example.
import subprocess
import tempfile
import shortuuid
import datetime
def run_with_schtasks_soon(s, delay=2):
"""
Run a program with schtasks with a delay of no more than
delay minutes and no less than delay - 1 minutes.
"""
# delay needs to be no less than 2 since, at best, we
# could be calling subprocess at the end of the minute.
assert delay >= 2
task_name = shortuuid.uuid()
temp_file = tempfile.NamedTemporaryFile(mode="w", suffix=".bat", delete=False)
temp_file.write('{}\nschtasks /delete /tn {} /f\ndel "{}"'.format(s, task_name, temp_file.name))
temp_file.close()
run_time = datetime.datetime.now() + datetime.timedelta(minutes=delay)
time_string = run_time.strftime("%H:%M")
# This is locale-specific. You will need to change this to
# match your locale. (locale.setlocale and the "%x" format
# does not seem to work here)
date_string = run_time.strftime("%m/%d/%Y")
return subprocess.run("schtasks /create /tn {} /tr {} /sc once /st {} /sd {}".format(task_name,
temp_file.name,
time_string,
date_string),
shell=True)
if __name__ == "__main__":
# Runs The Witness (if you have it)
run_with_schtasks_soon("start steam://rungameid/210970")
I'm making a Reddit bot that goes through comments on certain subreddits and replies to those with certain keyphrases.
I originally did not have a loop, and it worked fine, but I had to click run again every few minutes. I am running my python script on pythonanywhere.com, using PRAW.
import praw
import time
SECONDS_PER_MIN = 60
subreddit = reddit.subreddit('memes+dankmemes+comics+funny+pics')
keyphrase = ('Sauce+Sauce?')
def main():
while True:
for comment in subreddit.stream.comments():
if keyphrase in comment.body:
comment.reply('[Here.](https://www.youtube.com/watch?v=dQw4w9WgXcQ)\n\nI am a bot and this action was performed automatically. Learn more at [https://saucebot.com/](https://www.youtube.com/watch?v=dQw4w9WgXcQ)')
print('Posted!')
time.sleep(SECONDS_PER_MIN * 11)
if __name__ == '__main__':
main()
I expect it to respond to a random person who says "sauce" every 10 minutes, but now it won't respond to anyone.
Are you running your script on a PC? You could potentially use the task scheduler for that without using python at all. Just save your script as a binary using pyinstaller, then schedule it to run every ten minutes.
So I have this python code using the schedule python library. So that it can run other codes automatically on a daily basis at certain times. My school has these afs servers that us students can use and I was wondering if it was possible for me to run this code on the server than over having my labtop run 24/7.
import schedule
import time
import MTtest
import DataScrape
import MTSMScrape
schedule.every().day.at("11:00").do(MTtest.main)
schedule.every().day.at("11:00").do(DataScrape.main)
schedule.every().day.at("11:00").do(MTSMScrape.main)
while True:
schedule.run_pending()
time.sleep(1)
This is my code. I know it works. I am just asking if I can put it in the server and it will run no problem because I do not want my labtop on all the time. I would test it on my own but right now, I am having log in problems to the server and not sure how long it will take to fix it. So I would rather know now if this is possible. I imagine it should but maybe I am wrong. This is for a project I am working on. Thank you and appreciate the help.