~psycopg/psycopg/2.0.x

« back to all changes in this revision

Viewing changes to psycopg/pqpath.c

  • Committer: Federico Di Gregorio
  • Date: 2007-09-19 13:39:48 UTC
  • Revision ID: fog-10ac3c3a9634bc9f19ed31725d276fa79127f1c1
Aggressive threading on fetch (and a new bug!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
359
359
int
360
360
pq_is_busy(connectionObject *conn)
361
361
{
 
362
    int res;
362
363
    PGnotify *pgn;
363
364
 
364
365
    Dprintf("pq_is_busy: consuming input");
374
375
        return -1;
375
376
    }
376
377
 
377
 
    pthread_mutex_unlock(&(conn->lock));
378
 
    Py_END_ALLOW_THREADS;
379
378
 
380
379
    /* now check for notifies */
381
380
    while ((pgn = PQnotifies(conn->pgconn)) != NULL) {
384
383
        Dprintf("curs_is_busy: got NOTIFY from pid %d, msg = %s",
385
384
                (int) pgn->be_pid, pgn->relname);
386
385
 
 
386
        Py_BLOCK_THREADS;
387
387
        notify = PyTuple_New(2);
388
388
        PyTuple_SET_ITEM(notify, 0, PyInt_FromLong((long)pgn->be_pid));
389
389
        PyTuple_SET_ITEM(notify, 1, PyString_FromString(pgn->relname));
390
390
        PyList_Append(conn->notifies, notify);
 
391
        Py_UNBLOCK_THREADS;
391
392
        free(pgn);
392
393
    }
393
394
 
394
 
    return PQisBusy(conn->pgconn);
 
395
    res = PQisBusy(conn->pgconn);
 
396
    
 
397
    pthread_mutex_unlock(&(conn->lock));
 
398
    Py_END_ALLOW_THREADS;
 
399
 
 
400
    return res;
395
401
}
396
402
 
397
403
/* pq_execute - execute a query, possibly asyncronously
497
503
_pq_fetch_tuples(cursorObject *curs)
498
504
{
499
505
    int i, *dsize = NULL;
 
506
    int pgnfields;
 
507
    int pgbintuples;
500
508
 
501
 
    int pgnfields = PQnfields(curs->pgres);
502
 
    int pgbintuples = PQbinaryTuples(curs->pgres);
 
509
    Py_BEGIN_ALLOW_THREADS;
 
510
    pthread_mutex_lock(&(curs->conn->lock));
 
511
    
 
512
    pgnfields = PQnfields(curs->pgres);
 
513
    pgbintuples = PQbinaryTuples(curs->pgres);
503
514
 
504
515
    curs->notuples = 0;
505
516
 
506
517
    /* create the tuple for description and typecasting */
 
518
    Py_BLOCK_THREADS;
507
519
    Py_XDECREF(curs->description);
508
 
    Py_XDECREF(curs->casts);
 
520
    Py_XDECREF(curs->casts);    
509
521
    curs->description = PyTuple_New(pgnfields);
510
522
    curs->casts = PyTuple_New(pgnfields);
511
523
    curs->columns = pgnfields;
 
524
    Py_UNBLOCK_THREADS;
512
525
 
513
526
    /* calculate the display size for each column (cpu intensive, can be
514
527
       switched off at configuration time) */
515
528
#ifdef PSYCOPG_DISPLAY_SIZE
 
529
    Py_BLOCK_THREADS;
516
530
    dsize = (int *)PyMem_Malloc(pgnfields * sizeof(int));
 
531
    Py_UNBLOCK_THREADS;
517
532
    if (dsize != NULL) {
518
533
        int j, len;
519
534
        for (i=0; i < pgnfields; i++) {
534
549
        int fsize = PQfsize(curs->pgres, i);
535
550
        int fmod =  PQfmod(curs->pgres, i);
536
551
 
 
552
        Py_BLOCK_THREADS;
537
553
        PyObject *dtitem = PyTuple_New(7);
538
554
        PyObject *type = PyInt_FromLong(ftype);
539
555
        PyObject *cast = NULL;
622
638
        /* 6/ FIXME: null_ok??? */
623
639
        Py_INCREF(Py_None);
624
640
        PyTuple_SET_ITEM(dtitem, 6, Py_None);
 
641
    
 
642
        Py_UNBLOCK_THREADS;    
625
643
    }
626
644
 
627
 
    if (dsize) PyMem_Free(dsize);
 
645
    if (dsize) {
 
646
        Py_BLOCK_THREADS;
 
647
        PyMem_Free(dsize);
 
648
        Py_UNBLOCK_THREADS;
 
649
   }
 
650
   
 
651
    pthread_mutex_unlock(&(curs->conn->lock));
 
652
    Py_END_ALLOW_THREADS;
628
653
}
629
654
 
630
655
#ifdef HAVE_PQPROTOCOL3
856
881
            Py_END_ALLOW_THREADS;
857
882
        }
858
883
 
 
884
        Py_BEGIN_ALLOW_THREADS;
 
885
        pthread_mutex_lock(&(curs->conn->lock));
 
886
 
859
887
        Dprintf("pq_fetch: data is probably ready");
860
888
        IFCLEARPGRES(curs->pgres);
861
889
        curs->pgres = PQgetResult(curs->conn->pgconn);
 
890
 
 
891
        pthread_mutex_unlock(&(curs->conn->lock));
 
892
        Py_END_ALLOW_THREADS;
862
893
    }
863
894
 
864
895
    /* check for PGRES_FATAL_ERROR result */