1

I write my application to check performance when select values in MongoDB. And calculator time when complete.

But my application show result like random character and not down the line(\n). Like this:the images show my console when run application

Updated: I changed my code. But this also not working for me.

import sys
import os
from pymongo import MongoClient
import random
import pymongo
import time
from datetime import datetime
import multiprocessing

def mongoSelectStatement(result_queue):
    client = MongoClient('mongodb://localhost:27017')
    db = client.random

    cursor = db.random.find()

    for document in cursor:
        result_queue.put(document)

def main():
    processes = []
    result_queue = multiprocessing.Queue()
    startTime = datetime.now()
    for i in range(100):
        p = multiprocessing.Process(target=mongoSelectStatement, args=[result_queue])
        p.start()
        processes.append(p)

    print "Doi ket qua tra ve..."

    result = result_queue.get()

    for p in processes:
        p.terminate()
    endTime = datetime.now()
    print "Kqua tra ve: ", result, ' trong thoi gian ', (endTime - startTime)

if __name__ == '__main__':
    main()
Joe
  • 35
  • 1
  • 5

1 Answers1

0

Your MultiProcessing approach is going crazy. Before a print command finishes, the other process's print command starts printing output, resulting in garbage value. Either remove multiprocessing, running everything in sequence, or read about MultiProcessing map or Queue, that keeps track of job order, hence giving result in order. Check this for reference.

Another approach is to pass all output to a queue, wait for all jobs to finish executing and then print the result in sequence. This is better, as you won't have to edit a lot of your code. Just append value-set to a list, print at last. The only difference is, you'll see the output after sometime(when all jobs finish)

Community
  • 1
  • 1
Ankit Vadehra
  • 157
  • 1
  • 11
  • I updated my question. I must using multiprocessing to compared result. – Joe Jan 22 '16 at 09:36
  • @Joe Use Multiprocessing to perform operations., Use a sequential approach to just show the results. Else, look into Queue/Map that i have linked in my answer. That might help. – Ankit Vadehra Jan 22 '16 at 11:04
  • Thanks your reply. I was using Queue in updated code. – Joe Jan 22 '16 at 11:08