Django 1.6, Transaction.commit_on_success with Multiprocessing is not working

人走茶凉 提交于 2019-12-08 08:14:28

问题


I am new in Django and trying to write some test cases.

In my code I am doing some transaction. For that purpose I locked my that code using select_for_update in django. Now I want to test it whether lock is working correctly or not. I am running 2 or more processes simultaneously so that it allows only first process and wait here for finish first process and then other process proceeds.

#here XYZ and ABC are models.

@transaction.commit_on_success
def transaction_func():
    exp1 = ABC(a = 5)
    exp1.save()
    process_list =[]
    for i in xrange(2):
        p = Process(target=row_lock_method, args=('some_string',))
        p.start()
        time.sleep(3)
        process_list.append(p)

    for each in process_list:
        each.join()

    raise

def row_lock_method(code):
    exp2 = XYZ(b = code)
    exp2.save() 
    client = Client()
    client.login(username='gaurav@example.com', password='sample123')
    response = client.post('some_url',{'exp2':exp2},follow=True)

Here the above code is in other view file. So I used here cron job to run this file in django. I am calling row_lock_method twice in transaction_func using process. As test is run successfully but this is in real database so i want to rollback all changes done in this job so i put raise condition after both for loops.So that exception occurs here and it will rollback it through transaction.commit_on_success. but my problem is that the rollback is not working here. Even no error message is coming.

Is i am doing something wrong. Please do reply. Thanks in Advance.

来源:https://stackoverflow.com/questions/23541447/django-1-6-transaction-commit-on-success-with-multiprocessing-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!