数据库练习

一对多或者称为多对一

三张表:出版社,作者信息,书

一对多:一个出版社可以出版多本书

"""
create table books(id int primary key auto_increment,
name char(15) not null,
auther char(15) not null);

create table publisher(id int primary key auto_increment,
name char(15) not null,
book_id int,
foreign key (book_id) references books (id) on update cascade
);

create table students(id int primary key auto_increment,
name char(15) not null,cls_id int,
foreign key (cls_id) references cls (id)
on update cascade on delete cascade);

create table cls(id int primary key auto_increment,
name char(15) not null
);

insert into book(name) values
('九阳神功'),
('九阴真经'),
('九阴白骨爪'),
('独孤九剑'),
('降龙十巴掌'),
('葵花宝典')
;

insert into author_book(author_id,book_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(2,1),
(2,6),
(3,4),
(3,5),
(3,6),
(4,1)
;
"""

import time

from multiprocessing import Process

def task(name):

print(name)

if name=='main':

for i in range(10):

Process(target=task,args=(i,)).start() # 有几率不按顺序打印 证明并发

import time

from threading import Thread,current_thread,enumerate,active_count

def func(i):

time.sleep(1)

# print(i)

# print(current_thread().ident)

# for i in range(10):

# Thread(target=func,args=(i,)).start()

t = Thread(target=func,args=(1,))

t.start()

t.daemon = True

# print(t.ident)

# print(enumerate()) # [<_MainThread(MainThread, started 140735762297728)>, <Thread(Thread-1, started 123145353879552)>]

time.sleep(1) # 这个加了后active_count()结果就是1.

print(active_count())

from multiprocessing import Queue

import queue

q = queue.Queue(4) # 先进先出

q.put(1)

q.put(2)

q.put(3)

print(q.get())

import asyncio

async def func():

print('start time')

await asyncio.sleep(1)

print('end time')

loop = asyncio.get_event_loop()

loop.run_until_complete(func())

import asyncio

async def func():

print(1111)

await asyncio.sleep(1)

print(222)

loop = asyncio.get_event_loop()

loop.run_until_complete(func())

from gevent import monkey

monkey.patch_all()

并发的单例

class A:

from threading import Lock

import time

__instance = None

lock = Lock()

def new(cls, *args, **kwargs):

with cls.lock:

if not cls.__instance:

cls.time.sleep(0.000001)

cls.__instance = super().new(cls)

return cls.__instance

import time

import random

from multiprocessing import Process

def work(dic):

dic['count'] -=1

print(dic)

if name == 'main':

dic={'count':100}

for i in range(100):

p = Process(target=work,args=(dic,))

p.start()

time.sleep(3)

print('---->',dic)

原文地址:https://www.cnblogs.com/he-qing-qing/p/13599926.html