Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在气流中单独运行任务?_Python_Airflow_Airflow Scheduler - Fatal编程技术网

Python 如何在气流中单独运行任务?

Python 如何在气流中单独运行任务?,python,airflow,airflow-scheduler,Python,Airflow,Airflow Scheduler,我有一个要运行脚本的表列表。当我一次执行一个表时,它会成功运行,但当我尝试在任务上方执行for循环时,它会同时运行所有表,从而导致多个错误 这是我的密码: def create_tunnel_postgres(): psql_host = '' psql_port = 5432 ssh_host= '' ssh_port = 22 ssh_username = '' pkf = paramiko.RSAKey.from_private_key(S

我有一个要运行脚本的表列表。当我一次执行一个表时,它会成功运行,但当我尝试在任务上方执行for循环时,它会同时运行所有表,从而导致多个错误

这是我的密码:

def create_tunnel_postgres():

    psql_host = ''
    psql_port = 5432
    ssh_host= ''
    ssh_port = 22
    ssh_username = ''
    pkf = paramiko.RSAKey.from_private_key(StringIO(Variable.get('my_key')))

    server = SSHTunnelForwarder(
        (ssh_host, 22),
        ssh_username=ssh_username,
        ssh_private_key=pkf,
        remote_bind_address=(psql_host, 5432))

    return server

def conn_postgres_internal(server):
    """
    Using the server connect to the internal postgres
    """
    conn = psycopg2.connect(
        database='pricing',
        user= Variable.get('postgres_db_user'),
        password= Variable.get('postgres_db_key'),
        host=server.local_bind_host,
        port=server.local_bind_port,
    )

    return conn

def gzip_postgres_table(**kwargs):
    """

    path='/path/{}.csv'.format(table_name)
    server_postgres = create_tunnel_postgres()
    server_postgres.start()
    etl_conn = conn_postgres_internal(server_postgres)
    cur=etl_conn.cursor()
    cur.execute("""
        select * from schema.db.{} limit 100;
        """.format(table_name))
    result = cur.fetchall()
    column_names = [i[0] for i in cur.description]
    fp = gzip.open(path, 'wt')
    myFile = csv.writer(fp,delimiter=',')
    myFile.writerow(column_names)
    myFile.writerows(result)
    fp.close()
    etl_conn.close()
    server_postgres.stop()


#------------------------------------------------------------------------------------------------------------------------------------------------

default_args = {
    'owner': 'mae',
    'depends_on_past':False,
    'start_date': datetime(2020,1,1),
    'email': ['maom@aol.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 0,
    'retry_delay': timedelta(minutes=1)
}


tables= ['table1','table2']
s3_folder='de'
current_timestamp=datetime.now()



#Element'S VARIABLES

dag = DAG('dag1',
          description = 'O',
          default_args=default_args,
          max_active_runs=1,
          schedule_interval= '@once',
          #schedule_interval='hourly'
          catchup = False )


for table_name in pricing_table_name:
    t1 = PythonOperator(
        task_id='{}_gzip_table'.format(table_name),
        python_callable= gzip_postgres_table,
        provide_context=True,
        op_kwargs={'table_name':table_name,'s3_folder':s3_folder,'current_timestamp':current_timestamp},
        dag = dag)
有没有办法先运行表1..让它完成,然后运行表2?我试着在表中使用for表的名称:但没有用。任何想法或建议都会有所帮助。

您的for正在为您的表处理创建多个任务,这将在默认情况下并行执行任务

您可以将设置为1,也可以只创建1个任务并在任务内运行循环,然后同步执行。

如果您的for正在为表处理创建多个任务,则默认情况下,这将并行执行任务


您可以将设置为1,或者只创建1个任务并在任务内运行循环,然后同步执行。

我看到了您的代码,似乎您正在使用循环语句创建多个DAG任务,该语句并行运行任务

有一些方法可以达到你的要求

使用顺序执行器。 airflow.executors.sequential_executor.SequentialExecutor,仅按顺序运行任务实例

根据需要创建一个脚本。 创建一个scriptPython并将其用作PythonOperator,该PythonOperator将重复当前函数中的表数

将平行性限制为1。 您可以将airflow workers的afflow.cfg配置文件限制为1

步骤:

从airflow rootAIRFLOW\u主页打开airflow.cfg

设置/更新并行度=1

重新启动气流


这应该可以工作。

我看到了您的代码,看起来您正在使用循环语句创建多个DAG任务,该语句并行运行任务

有一些方法可以达到你的要求

使用顺序执行器。 airflow.executors.sequential_executor.SequentialExecutor,仅按顺序运行任务实例

根据需要创建一个脚本。 创建一个scriptPython并将其用作PythonOperator,该PythonOperator将重复当前函数中的表数

将平行性限制为1。 您可以将airflow workers的afflow.cfg配置文件限制为1

步骤:

从airflow rootAIRFLOW\u主页打开airflow.cfg

设置/更新并行度=1

重新启动气流


这应该行得通。

我认为有三种方法可以解决这个问题

在afflow.cfg文件中限制平行度=1。 创建一个python代码,该代码将通过表和 用python调用它 创建一个池并为其分配一个插槽。
我认为有三种方法可以解决这个问题

在afflow.cfg文件中限制平行度=1。 创建一个python代码,该代码将通过表和 用python调用它 创建一个池并为其分配一个插槽。
我想你需要这样的达格

代码:

from datetime import datetime
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

import sys
sys.path.append('../')
from mssql_loader import core #program code, which start load 
from mssql_loader import locals #local variables, contains dictionaries with name
def contact_load(typ,db):

    core.starter(typ=typ,db=db)
    return 'MSSQL LOADED '+db['DBpseudo']+'.'+typ

dag = DAG('contact_loader', description='MSSQL sqlcontact.uka.local loader to GBQ',
          schedule_interval='0 7 * * *',
          start_date=datetime(2017, 3, 20), catchup=False)

start_operator = DummyOperator(task_id='ROBO_task', retries=3, dag=dag)


for v in locals.TABLES:
    for db in locals.DB:        
        task = PythonOperator(
            task_id=db['DBpseudo']+'_mssql_' + v, #create Express_mssql_fast , UKA_mssql_important and etc
            python_callable=contact_load,
            op_kwargs={'typ': v,'db':db},
            retries=3,
            dag=dag,
        )

        start_operator >> task #create parent-child connection to from first task to other

我想你需要这样的达格

代码:

from datetime import datetime
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

import sys
sys.path.append('../')
from mssql_loader import core #program code, which start load 
from mssql_loader import locals #local variables, contains dictionaries with name
def contact_load(typ,db):

    core.starter(typ=typ,db=db)
    return 'MSSQL LOADED '+db['DBpseudo']+'.'+typ

dag = DAG('contact_loader', description='MSSQL sqlcontact.uka.local loader to GBQ',
          schedule_interval='0 7 * * *',
          start_date=datetime(2017, 3, 20), catchup=False)

start_operator = DummyOperator(task_id='ROBO_task', retries=3, dag=dag)


for v in locals.TABLES:
    for db in locals.DB:        
        task = PythonOperator(
            task_id=db['DBpseudo']+'_mssql_' + v, #create Express_mssql_fast , UKA_mssql_important and etc
            python_callable=contact_load,
            op_kwargs={'typ': v,'db':db},
            retries=3,
            dag=dag,
        )

        start_operator >> task #create parent-child connection to from first task to other

在配置文件中,您知道如何编辑以限制工作人员的数量吗?在config.yaml文件中设置parallelism=1?在afflow.cfg文件中,您在哪里找到它?在配置文件中,您知道如何编辑以限制工作人员的数量吗?在config.yaml文件中设置parallelism=1?在afflow.cfg文件中,您在哪里找到它?