Re: [Tutor] Python Daemons

2017-08-02 Thread dbosah
Also here's the link to the tutorial
https://youtu.be/WrtebUkUssc
 Original message From: Steven D'Aprano <st...@pearwood.info> 
Date: 8/1/17  8:49 PM  (GMT-05:00) To: tutor@python.org Subject: Re: [Tutor] 
Python Daemons 
Hi Daniel,

My responses below.

On Tue, Aug 01, 2017 at 02:48:19PM -0400, Daniel Bosah wrote:
> I'm following an online tutorial about threading. This is the code I've
> used so far:

Can you give us a link to the tutorial?

[...]
> def portscan(port):
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> try:
>    con = s.connect((target,port)) # con for connect
>    with print_lock: # if sucessful run with statement
>   print 'port', port, 'is open'
>    con.close() # closes connection
> except:
>    pass #if it doesn't work pass the method

I'm very concerned about that bare "except" line. I'm not absolutely 
saying that it is wrong, but in general bare excepts are a terrible 
idea.

https://realpython.com/blog/python/the-most-diabolical-python-antipattern/


> def threader():
> while True:
> worker = q.get()
> portscan(worker)
> q.task_done
> q = Queue()
> for x in range(30):
> t = threading.Thread(target = threader() #creates a thread, gets
> workers from q, set them to work on portscanning
> t.daemon() = True # want it to be a daemon
> t.start()
> #jobs = ports
> for worker in range(1,101): # port zero invalid port
> q.put(worker) # puts worker to work
> q.join() #waits till thread terminiates
> 
> 
> I don't know what a Daemon is, 

https://en.wikipedia.org/wiki/Daemon_(computing)




> and I also don't know how to use it in
> Python 2.7. Apparently its built in Python 3, but  I don't know how to use
> it in Python 2.7. Any help would be appreciated.

What happens when you try?



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-02 Thread Cameron Simpson

On 01Aug2017 14:48, Daniel Bosah  wrote:

I'm following an online tutorial about threading. This is the code I've
used so far:


In addition to the other replies, which mention the general computing "daemon" 
notion and the "python-daemon" library which aids making such python programs 
into well behaved daemons, I've got a couple of remarks about your code:


[...]

def threader():
   while True:
   worker = q.get()
   portscan(worker)
   q.task_done


task_done is a method. So "q.task_done()". Your code will parse and run but the 
method will not get called; "q.task_done" with no brackets just mentioned the 
method without running it (sometimes a program wants to talk about a method or 
function but not call it right now).



   t.daemon() = True # want it to be a daemon


The Thread.daemon is a property, not a method. So you set it like this:

   t.daemon = True

In the context of a thread, the daemon attribute implies that the daemon is a 
"worker" process, which does not need special shutdown. When your program ends, 
the Python runtime does not actually terminate until all _non_ damon Threads 
have finished. By marking a Thread as a daemon you're saying that its activity 
is not important after program exit. This is probably not the case for your 
tutorial task.


Like others, I recommend learning Python 3. It is broadly the same language but 
it is current.


Cheers,
Cameron Simpson  (formerly c...@zip.com.au)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-01 Thread Ben Finney
Steven D'Aprano  writes:

> On Tue, Aug 01, 2017 at 09:03:30PM -0400, dbosah wrote:

(I am not seeing your replies in this forum, dbosah. Please address your
replies to the mailing list – not to an individual – if you want the
discussion to continue.)

> > And I'm still confused by the definition of a Daemon.

The term is intended to distinguish a daemon from a typical process.

What understanding do you already have of a typical process on the
operating system? Does the term “process” mean anything? How about
“terminal”?

If you don't have a solid understanding of that, the definition is
unlikely to help because it's making distinctions below the level you're
used to thinking about.

Did the Wikipedia article help?

> In summary, and Ben will correct me if I'm wrong, a daemon is a
> computer program that runs as a background task.

That's not wrong, but “background task” is vague. I don't know what to
assume about dbosah's understanding of processes to know how to help.

I'd say: what characterises a daemon process is that it has no
controlling terminal (it is “detached” from the terminal that initiated
it).

-- 
 \  “As soon as we abandon our own reason, and are content to rely |
  `\   upon authority, there is no end to our troubles.” —Bertrand |
_o__)Russell, _Unpopular Essays_, 1950 |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-01 Thread Steven D'Aprano
On Tue, Aug 01, 2017 at 09:03:30PM -0400, dbosah wrote:

> It usually says that it's an error.

Sorry, my crystal ball is at the shop being repaired and I don't know 
what "an error" means or how to fix it.

Please COPY AND PASTE the full text of the error, starting with the 
line "Traceback" and ending with the error message. Don't summarise it, 
or re-type it from memory.

The Python interpreter provides a lot of useful debugging information in 
these tracebacks. If you ignore it, and just say "An error occurred", it 
is impossible to tell what the error is and why it happened. Your code 
has over 20 lines in it. Assuming a minimum of ten things could go wrong 
with each line, that's 200 possible errors. At minimum.

It is a waste of my time, and confusing for you, to try to guess which 
of those is the error, when you have the information right there in 
front of you.

If you post the actual Traceback, we can help. If you don't, we can't.

> And I'm still confused by the definition of a Daemon. Is there another 
> resource you know that I can look at to break it down even further?

Have you tried googling for "daemon"?

In summary, and Ben will correct me if I'm wrong, a daemon is a computer 
program that runs as a background task. If you come from Windows, think 
of your anti-virus scanner which scans every file when you double-click 
on it, before the file runs. That will probably be a daemon, or the 
closest equivalent.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-01 Thread Steven D'Aprano
On Tue, Aug 01, 2017 at 09:05:02PM -0400, dbosah wrote:
> Also here's the link to the tutorial
> https://youtu.be/WrtebUkUssc

That is clearly marked as "Python 3 Programming Tutorial". Why are you 
using Python 2? When you have a problem, you won't know if the problem 
is with your code, or a difference between Python 2 and 3. You're just 
making a rod for your own back by using Python 2.7 with a 3 tutorial.

Save yourself a lot of heartache and either stick to Python 2 tutorials 
or upgrade to Python 3.

-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-01 Thread Ben Finney
Daniel Bosah  writes:

> I'm following an online tutorial about threading.

Can you point to it so we can understand your specific situation?

> I don't know what a Daemon is

The package ‘python-daemon’ is a library to build your program as a
daemon .

The referenced PEP 
describes what a daemon process is, and how the library provides what
you need to implement a daemon process.

> and I also don't know how to use it in Python 2.7. Apparently its
> built in Python 3, but I don't know how to use it in Python 2.7.

If you are learning Python, you should not use Python 2 unless there is
no other option. Python 2 is on the way out – it will stop receiving all
support after 2020, and is already far behind Python 3.

There are, of course, tutorials for Python 3 and threading. For example
.

> Any help would be appreciated.

I hope that helps.

-- 
 \“The supreme vice is shallowness.” —Oscar Wilde, _De |
  `\  Profundis_, 1897 |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Daemons

2017-08-01 Thread Steven D'Aprano
Hi Daniel,

My responses below.

On Tue, Aug 01, 2017 at 02:48:19PM -0400, Daniel Bosah wrote:
> I'm following an online tutorial about threading. This is the code I've
> used so far:

Can you give us a link to the tutorial?

[...]
> def portscan(port):
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> try:
>con = s.connect((target,port)) # con for connect
>with print_lock: # if sucessful run with statement
>   print 'port', port, 'is open'
>con.close() # closes connection
> except:
>pass #if it doesn't work pass the method

I'm very concerned about that bare "except" line. I'm not absolutely 
saying that it is wrong, but in general bare excepts are a terrible 
idea.

https://realpython.com/blog/python/the-most-diabolical-python-antipattern/


> def threader():
> while True:
> worker = q.get()
> portscan(worker)
> q.task_done
> q = Queue()
> for x in range(30):
> t = threading.Thread(target = threader() #creates a thread, gets
> workers from q, set them to work on portscanning
> t.daemon() = True # want it to be a daemon
> t.start()
> #jobs = ports
> for worker in range(1,101): # port zero invalid port
> q.put(worker) # puts worker to work
> q.join() #waits till thread terminiates
> 
> 
> I don't know what a Daemon is, 

https://en.wikipedia.org/wiki/Daemon_(computing)




> and I also don't know how to use it in
> Python 2.7. Apparently its built in Python 3, but  I don't know how to use
> it in Python 2.7. Any help would be appreciated.

What happens when you try?



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Daemons

2017-08-01 Thread Daniel Bosah
I'm following an online tutorial about threading. This is the code I've
used so far:

import socket
import threading
from queue import Queue



print_lock = threading.Lock()

target = 'pythonprogramming.net'

def portscan(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
   con = s.connect((target,port)) # con for connect
   with print_lock: # if sucessful run with statement
  print 'port', port, 'is open'
   con.close() # closes connection
except:
   pass #if it doesn't work pass the method

def threader():
while True:
worker = q.get()
portscan(worker)
q.task_done
q = Queue()
for x in range(30):
t = threading.Thread(target = threader() #creates a thread, gets
workers from q, set them to work on portscanning
t.daemon() = True # want it to be a daemon
t.start()
#jobs = ports
for worker in range(1,101): # port zero invalid port
q.put(worker) # puts worker to work
q.join() #waits till thread terminiates


I don't know what a Daemon is, and I also don't know how to use it in
Python 2.7. Apparently its built in Python 3, but  I don't know how to use
it in Python 2.7. Any help would be appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor