alcemirfernandes / pynguin

Automatically exported from code.google.com/p/pynguin
GNU General Public License v3.0
0 stars 0 forks source link

threaded operation of pynguins? #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be great to be able to do something like:

def circle(penguin):
    while True:
        penguin.fd(1)
        penguin.rt(1)

and then be able to call p.circle() and q.circle() to have both operate at the 
same time.  This is especially nice in the context of pursuit problems like 
http://www.math.nmsu.edu/~breakingaway/Lessons/bugs1/bugs.html

What version of the product are you using? On what operating system?
HEAD, Ubuntu 9.10

Original issue reported on code.google.com by aresnick...@gmail.com on 6 Jul 2010 at 6:45

GoogleCodeExporter commented 9 years ago
It should work. Have you looked at the threaded example file?

The bugs thing looks a lot like the follow example in the multi example file.

Thinking about it now, I was wondering if there might be a way to start one of 
the pynguins running in a thread a get the prompt back in the interpreter, but 
I'm not sure how to do that right now.

The thing is, even at a regular python interpreter prompt (running in bash) 
starting a thread does not return control to the console.

Original comment by miss...@hotmail.com on 6 Jul 2010 at 8:32

GoogleCodeExporter commented 9 years ago
Ah, yeah!  I hadn't even noticed it in the examples file. . .thanks!  It would 
be nice, re: the prompt, but I'm not sure how to do that, either. . .

Original comment by aresnick...@gmail.com on 7 Jul 2010 at 12:31

GoogleCodeExporter commented 9 years ago
# Just to be clear, I don't believe it is necessary to
#   use threads to control multiple pynguins as in the
#   bugs activity. That can be seen in the follow* examples
#   in the multi.pyn example file

# However, here is how you could use threads to
# control multiple pynguins ...

def tcmd(cmd, *args, **kw):
    import threading
    thr = threading.Thread(target=cmd, args=args, kwargs=kw)
    thr.start()

def tcircle(penguin):
    while True:
        penguin.fd(1)
        penguin.rt(1)

def allcircle():
    for pyn in pynguins:
        tcmd(tcircle, pyn)

>>> [Pynguin() for i in range(5)]
>>> allcircle()

# In fact (somewhat amazingly) you actually DO get the
# prompt back after starting a thread, so you could do
# it more like this:

>>> tcmd(tcircle, p)  # start one going
>>> q = Pynguin() # create a new one
>>> tcmd(tcircle, q) # and start it circling

# The real problem now is, I have no idea how to STOP
#   the code that is running in a thread...

# So, I am going to change the focus of this bug to figuring
# out how to find and terminate all active threads, if possible.

Original comment by miss...@hotmail.com on 12 Jun 2011 at 9:27

GoogleCodeExporter commented 9 years ago
I have added these examples to the threaded.pyn example file, including an 
example "bugs" implementation using threads. I have also added a bugs that does 
not require threads in the multi.pyn example file.

Original comment by miss...@hotmail.com on 25 Jun 2011 at 3:41