Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python Threads (You're excited already,)

Name: Anonymous 2012-02-16 0:52


####################################################
# uses simple shared global data (not mutexes) to
# know when threads are done in parent/main thread;
####################################################

import thread
stdoutmutex = thread.allocate_lock( )
exitmutexes = [0] * 10

def counter(myId, count):
    for i in range(count):
        stdoutmutex.acquire( )
        print '[%s] => %s' % (myId, i)
        stdoutmutex.release( )
    exitmutexes[myId] = 1  # signal main thread

for i in range(10):
    thread.start_new(counter, (i, 100))

while 0 in exitmutexes: pass
print 'Main thread exiting.'




[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 2
[9] => 2
[0] => 3
~~~ ... BunchAShitYouCareEvenLessAbout ... ~~~
[6] => 98
[7] => 98
[8] => 98
[9] => 98
[0] => 99
[1] => 99
[2] => 99
[3] => 99
[4] => 99
[5] => 99
[6] => 99
[7] => 99
[8] => 99
[9] => 99
Main thread exiting.


So I don't get this. Why does it count the numbers in order? Wouldn't a counter thread acquire itself as soon as it released itself? I don't understand why any of this is in order. I get why the processes can be out of order, but not why it counts in order.

Name: Anonymous 2012-02-16 1:44

Because you opened up threads that process in order.

/thread

Name: Anonymous 2012-02-17 19:26

>>2
But why the would the order I opened the threads in matter? They're threads, it switches between them once the lock's released.

Name: Anonymous 2012-02-17 19:50

bump

Name: Anonymous 2012-02-17 20:11

Your code is shit, not going to read it, also Jackson five get.

Name: Anonymous 2012-02-17 22:47

>>5
It's not even mine, it's from a book.

You just can't understand it. Go back to scrubbing toilets, you mental midget!

Name: Anonymous 2012-02-18 0:21

Awesome pics. Great size. Look thick. Solid. Tight. Keep us all posted on your continued progress with any new progress pics or vid clips. Show us what you got man. Wanna see how freakin' huge, solid, thick and tight you can get. Thanks for the motivation.

Name: Anonymous 2012-02-18 0:53

>>6
The book you're reading is shit.

Name: Anonymous 2012-02-18 7:13

>>1
Ew.
lrn2Python3

Name: Anonymous 2012-02-18 12:06

>>9
Python 3 is a fucking joke.

Name: Anonymous 2012-02-18 12:15

>>10
I hear that a lot, but I don't know why.

Is it just a lack of libraries?

Name: Anonymous 2012-02-18 19:14

>>11
Lack of sanity, Guido has just changed how everything works instead of giving new and clear names. So now all the people who use Python 2.7 can't tell what a piece of Python 3 code is doing at a glance. Which is exactly why nobody is going to use it ever, no point in learning what's effectively a different language especially when there's no gain in doing so since no existing libraries are compatible with Python 3 so all that's left is the FIOC, which quite frankly is shit.

Name: Anonymous 2012-02-18 20:02

>>12
Guido
YEAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHH BUDDDDDDDDDDDDDDYYYYYYYYYYYYYYYYYY

LOOKS LIKE THERE'S A SITUATION UP IN HERE RIGHT NOW.

Name: Anonymous 2012-02-18 20:10

>>13
FCP
FISTPUMP
CHAPSTICK
PUSHUPS

YEAHHHHHHHHHHHHHH BUDDY

Name: Anonymous 2012-02-19 9:59

>>3

>>2 here... I gave time to let the trolls have at it. Anyway, here's the breakdown.

You opened threads.... that process... in order. Now I know that was complicated, so here's a further break down.

Your code opens up 10 threads with 100 jobs each. Those threads process in order. Which works like this:

Thread #1: Job #1
Thread #2: Job #1
Thread #3: Job #1

And so on and so forth until all the jobs are done. See, the cool part about languages that try forcing OOP... like python. Is that your going to spend all your programming time using somebody else's shit library. Like what your doing now. Ergo, if said library doesn't do what you want... too fucking bad. And even if you do write your own library it won't be considered "pythonic". No no no, no matter what. Don't question how much these neckbeards love tearing up somebody else's shit. Yes, I'm stating clearly that it doesn't matter if a library works or not... just so long as the library is written out in a way that people will accept socially. Also.. yes, I'm very much aware that I didn't post functioning code that did what you wanted... twice now.

inb4 shitstorm over talking about forced OOP

Name: Anonymous 2012-02-19 13:14

>>12
I don't know shit about Python but I want to learn it, because some libs look useful (SciPy, for example).

If I never did anything in python, should I learn 2.7 or 3?

Name: Anonymous 2012-02-19 14:12

>>15
So a job would be defined as every time a thread acquires and releases a lock?

And what it does is finish a job in every current thread before it can do the next job in a previous thread.

Name: Anonymous 2012-02-19 15:05

>>16
Learn Python 2.7, it actually has users, libraries and jobs.

Name: Anonymous 2012-02-19 15:13

>>15
I read the last bit as ``forced POOP''.

Name: Anonymous 2012-02-19 15:24

ONE WORD GLOBAL INTERPRETER LOCK THREAD OVER

Name: Anonymous 2012-02-19 15:27

>>1
I think you'll have to part with the ``explanation'' that it's implementations specific behavior, because ideally (actually, supposing that the machine in question would capable of multiprocessing) any of the 9 threads could attempt to grab the lock in unison, and whatever happens will be a detail of how the OS schedules processes or whatever.

And considering that you are running on a machine which happens to be the retarded Python VM written by Guido, we get to the fun part where we point and laugh at its implementation which only allows one thread to run at a time in the first place. HA-HA!

Name: Anonymous 2012-02-19 15:31

>>20
I believe you meant

ONE WORD, THE FORCED GLOBAL LOCKING OF THE INTERPRETER, THREAD OVER

Name: Anonymous 2012-02-19 21:21

>>2 & >>15 here.

>>19

That will happen.

>>17

DING DING DING.

Okay, since is the third time I'll break down and post some code.

#!/usr/bin/env python

from threading import Thread
exitmutexes = [0] * 10

def counter(myId, count):
    for i in range(count):
        print '[%s] => %s' % (myId, i)
    exitmutexes[myId] = 1  # signal main thread

for i in range(10):
    t = Thread(target=counter, args=(i,100,))
    t.start()

while 0 in exitmutexes: pass
print 'Main thread exiting.'


Run it a couple of times to check the output. It prints all types of out of order because it processes whatever it wants whenever it gets it's hands on it.

inb4 didn't color code, not pythonic, or other such neckbeard shitstorm

Name: Anonymous 2012-02-19 22:02

>>23
while 0 in exitmutexes: pass

wow, a spin join. Who would have thought such a thing could be?

Name: Anonymous 2012-02-19 22:07

>>24
typical ``pythonista'' solution. should have just used node.js like a competent programmer

Name: Anonymous 2012-02-19 22:13

Under the assumption that threading is imported.

for thread in threading.enumerate():
    if thread is not threading.currentThread():
        thread.join()

Name: Anonymous 2012-02-20 8:33

Learn D

Name: Anonymous 2012-02-20 8:50

Have any of you read Mike Dawson's "Python for the Absolute Beginner?"

Name: Anonymous 2012-02-20 9:45

Name: Anonymous 2012-02-20 9:52

>>29
?

Name: Anonymous 2012-02-20 11:23

Python us tyed to my little finger with a magical red syringe <3 that's the only kind ofthread I need.

--
Sent from my iPhone

Name: Anonymous 2012-02-20 12:55

>>23
Good, I understand now. There was one small hiccup though.


import sys
from threading import Thread
exitmutexes = [0] * 10

def counter(myId, count):
    for i in range(count):
        sys.stdout.write("[%s] => %s\n" % (myId, i))
    exitmutexes[myId] = 1 #Signal main thread

for i in range(10):
    t = Thread(target = counter, args = (i, 100,))
    t.start()

while 0 in exitmutexes: pass
print "Main thread exiting"


I had to replace print with a sys.stdout.write because the print statement doesn't properly support threads and gives me garbled new lines if I try to use it. Besides that though everything worked dandy.

Did you get that code from O'Reily's "Programming Python"? It looks similar.

Name: Anonymous 2012-02-20 20:35

>>2,>>15, >>23 here.

>>32

Not sure why your getting print errors. Then again, I only ran the code a dozen or so times.

Also, the code comes from... not wanting to use shitty libraries, and using something that works instead. No, seriously.. This was covered in post #2. Python has a lot of shitty libraries. And a lot more shitty methods to use said aforementioned shitty libraries. As for threads in python on a generalized scale/scope; there's around 9 different ways to do this. I could give a fuck less about 7 of those methods.

Final Summation:

If.. IF, you want to use python. Your going to have to go through multiple libraries to find out what each can do. No Exceptions.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List