Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
threading
#1
Hi,

I have this code, which monitors folders for incoming files:

import os
import glob
import time
import threading
import smtplib

smtpserver = ''
smtpserverport = 587
fromaddress = ''
fromaddressusername = ''
fromaddresspassword = ''
toaddress = ''

class Sc:


    def __init__(self, name='Sc'):
        """ you may enter a name when instantiation"""
        self.name = name
        print('* ' * 3 + ' DAEMON STARTED' + ' *' * 3)
        print('[+] Instance name: ' + name)

    def getlastwav(self):
        pass

    def countfiles(self, folder):
        pass

    def sendmail(self, to):
        print('[+] email sent to ' + to)

    def checkfornewwav(self, folder='.',refresh=5):
        """ checks for new wav files, and sends email """
        print('[+] Folder to watch: ' + folder)
        print('[+] Refresh time: ' + str(refresh) + ' sec')
        while True:
            before = self.countfiles(folder)
            time.sleep(refresh)
            after = self.countfiles(folder)
            if before < after:
                print('[+] Last file: ' + self.getlastwav())
                self.sendmail('[email protected]')


threading.sc1 = Sc('first')
threading.sc1.checkfornewwav('.', 10)

threading.sc2 = Sc('second')
threading.sc2.checkfornewwav('.', 12)
I'd like the instantiations pop up in new terminal, moreover the second instantiation never gets instantiated this way.
Can you tell me how I could run such a daemon on different folders in different terminals with using CPU threading?

Thanks
Reply
#2

  1. I don't even see how you expect the threads to run as threads with this code. Where are the threading.Thread objects created?
  2. AFAIK you cannot have several standard terminal windows using a single process. If you want several windows, you have to create a grapĥical UI, or start each instance as a separate process, using whatever OS/Desktop manager-specific command is necessary to start a process in a new terminal (or as a new tab in the current console window).
IMHO starting a new thread process per directory is a waste, you can monitor a whole list of directories with one process. What benefit are you expecting from multiple threads?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Hi. I just want 1-2-3 instances pop up in new terminal windows when i run this code.
Each folder would be watched in different terminals started at the very moment.
Now it's done by thee different scipts started one after each. Id like the editing in one place, and have the ability to configure the refresh time on each folder like this at the end of the script:
sc1 = Sc('first')
sc1.checkfornewwav(r'C:\testing\sc1', 10)
 
sc2 = Sc('second')
sc2.checkfornewwav(r'C:\testing\sc2', 12)

sc3 = Sc('third')
sc3.checkfornewwav(r'C:\testing\sc3', 15)
The first parameter is the folder to be watched, the second is the refresh time. Razz
I think threading would be nice to do this, since I would like to start the daemon on each folder a exactly the same time.
Reply
#4

  1. You didn't answer the first question
  2. Threading is meant for exactly the opposite: let various parts of the code run asynchronously and independently of each other. And you can 't get true multi-threading (ie, actual concurrent execution) in Python anyway.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
There is multiprocessing module.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Mar-13-2017, 09:04 PM)Ofnuts Wrote:
  1. You didn't answer the first question
  2. Threading is meant for exactly the opposite: let various parts of the code run asynchronously and independently of each other. And you can 't get true multi-threading (ie, actual concurrent execution) in Python anyway.

I can't explain more than that I did. I don't know how to create thread objects. I'd like to start 1-2-3-4 instances pop up in new python terminals. That's all. I don't like the way it is run now 1 script for each folder...
If it's not threading, it's fine.

My question is: when instantiation happens (sc1 = Sc() etc.) how can those instantiations open up in new terminal windows.
Reply
#7
You Sc() things are not threads. They either have to derive the Thread class, or be passed in the run() method of a freshly created Thread object.

But if you use threads forget the separate terminals. If you use processes this can perhaps be done, depending on OS and desktop manager (on my Linux I would launch them as separate tabs in a terminal window)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,814 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,120 Oct-01-2021, 08:32 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020