How to add lock in multi-mechanize

54 views
Skip to first unread message

Neil Xu

unread,
Mar 6, 2014, 4:34:52 AM3/6/14
to multi-m...@googlegroups.com

Hi all:
   the following "user_name" can only be used once, so it must be unique.
   In multiprocess, we can use lock.
   How could i ensure "seq" to be unique in multi-mechanize?

Thanks
Neil
   


import urllib
import urllib2
import random
import time
import json

numbers = range(1, 10001)
prefix = 'test0306_10_'
url = 'http://test/'
used = []

class Transaction(object):
    def __init__(self):
        pass

    def run(self):
        for i in range(10):
            seq = numbers.pop(random.randint(1,len(numbers) - 1000))
            if seq not in used:
                break
        used.append(seq)
        print "\n{0}--".format(seq)
        query_args = { 
            'mx_email': '{0}{1}@126.com'.format(prefix, seq), 
            'user_name': '{0}{1}'.format(prefix, seq),
            'mx_pwd': '111111', 
          
        } 
        
        start = time.time()
        encoded_args = urllib.urlencode(query_args)
        text =  urllib2.urlopen(url, encoded_args).read()  
        response_time = time.time()
        result = json.loads(text)

        if result['result'] is not True:
            raise Exception('{0}--{1}'.format(str(result),str(seq)))
        self.custom_timers['register_web'] = response_time - start


if __name__ == '__main__':
    trans = Transaction()
    trans.run()
    for timer in ('request sent', 'response received', 'content transferred'):
        print '%s: %.5f secs' % (timer, trans.custom_timers[timer])

Reply all
Reply to author
Forward
0 new messages