Skip to content

How to use cx_OracleSessionPool in multiprocess while can't pickle cx_OracleSessionPool objects #541

@bestathena

Description

@bestathena
  1. What versions are you using?

Database version: 12c
platform.platform: Linux-redhat 7.8,windows 7
platform.python_version: 3.6.8
cx_Oracle.version: 8.1.0

  1. Describe the problem

I want to share OracleSessionPool in multiprocesses to do data etl works . There are many jobs so I don't want to create db conncet in each job ,but the python can't pickle cx_OracleSessionPool objects when pass OracleSessionPool to subprocess.

if I can't create a session pool in one process and then acquire a connection from that pool in a worker process, what is the purpose of cx_oracle sessionpool ? How can i make a db conn when intitialization subprocess and close it before subprocess (not worker method ) quit

there are about 1000 data etl jobs , each execute time varies from 1 minute to 20 minutes.
the db server is 8C32G, so I plan to open 8 processes.
I know the db workload is not very heavy for now ,even if open and close a connect in each subprocess func, but in consideration of rubust and expansibility, I try to use an elegent method to take db connet in subprocess

  1. Include a runnable Python script that shows the problem.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from multiprocessing import Pool
import cx_Oracle


def worker(job_id,conn_pool):
    print('sub process :' + str(job_id) + 'start')
    conn = conn_pool.acquire()
    # do something with db
    conn_pool.release(conn)

def worker_error(info):
    print('run_error:'+ str(info))

if __name__ == '__main__':
    process_pool = Pool(5)

    conn_pool = cx_Oracle.SessionPool("rpsdb", 'rpsdb', "11.12.4.51:1521/rpsdb", min=2, max=5, increment=1, encoding="UTF-8")

    for i in range(5):
        process_pool.apply_async(func=worker, args=(i, conn_pool,),error_callback=worker_error)

    process_pool.close()
    process_pool.join()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions